Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. <?php
  2.  
  3. class NameObj {
  4.  
  5. public $name;
  6.  
  7. function __construct($name){
  8. $this->name = $name;
  9. }
  10. }
  11.  
  12. class IdObj{
  13.  
  14. public $id;
  15.  
  16. function __construct($id){
  17. $this->id = $id;
  18. }
  19. }
  20.  
  21. $idArray = array(
  22. new IdObj(1),
  23. new IdObj(2),
  24. new IdObj(3)
  25. );
  26.  
  27. $nameArray = array(
  28. new NameObj('1 - Object 1 Name'),
  29. new NameObj('2 - Object 2 Name')
  30. );
  31.  
  32. function custom_diff($oId, $oName){
  33. $splitName = explode(' - ', $oName->name);
  34. $idFromName = $splitName[0];
  35.  
  36. $id = $oId->id;
  37.  
  38. if($idFromName == $id) return 0;
  39. return $idFromName > $id ? 1 : -1;
  40. }
  41.  
  42. $missing_objects = array_udiff($idArray, $nameArray, 'custom_diff');
  43.  
  44. print_r($missing_objects);
  45.  
  46. ?>
  47.  
  48. PHP Notice: Undefined property: IdObj::$name in /home/ubuntu/test2.php on line 33
  49. PHP Notice: Undefined property: IdObj::$name in /home/ubuntu/test2.php on line 33
  50. PHP Notice: Undefined property: NameObj::$id in /home/ubuntu/test2.php on line 36
  51. PHP Notice: Undefined property: IdObj::$name in /home/ubuntu/test2.php on line 33
  52. PHP Notice: Undefined property: IdObj::$name in /home/ubuntu/test2.php on line 33
  53. Array
  54. (
  55. [1] => IdObj Object
  56. (
  57. [id] => 2
  58. )
  59.  
  60. [2] => IdObj Object
  61. (
  62. [id] => 3
  63. )
  64.  
  65. )
  66.  
  67. $extractor = function($o) { return explode(' - ', $o->name)[0]; };
  68. $idsFromNames = array_flip(array_map($extractor, $nameArray));
  69.  
  70. foreach ($idArray as $k => $o) {
  71. if (!isset($idsFromNames[$o->id])) {
  72. unset($idArray[$k]);
  73. }
  74. }
  75.  
  76. function custom_diff($oId, $oName)
  77. {
  78. $one = $oId instanceof IdObj?$oId->id:explode(' - ', $oId->name)[0];
  79. $two = $oName instanceof NameObj?explode(' - ', $oName->name)[0]:$oName->id;
  80. return $one-$two;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement