Advertisement
rfv123

/34462068/compare-two-arrays-and-extract-difference

Dec 25th, 2015
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.93 KB | None | 0 0
  1. <?php // http://stackoverflow.com/questions/34462068/how-to-compare-two-associative-arrays-and-remove-the-matching-elements-from-one
  2.  
  3. // include __DIR__ . '/__bootstrap__.php';
  4. /*
  5.  * Convert Print_R back into an array...
  6.  *
  7.  * http://phillihp.com/toolz/php-array-beautifier/
  8.  */
  9.  
  10. $sA = 'a:5:{i:0;a:10:{s:7:"user_id";s:3:"109";s:15:"profile_page_id";s:1:"0";s:9:"user_name";s:11:"profile-109";s:9:"full_name";s:11:"Hiten Patel";s:6:"gender";s:1:"0";s:10:"user_image";s:4:"\"\"";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"6";s:11:"language_id";s:1:"0";s:13:"profile_image";s:92:"http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile_100.png";}i:1;a:10:{s:7:"user_id";s:4:"1585";s:15:"profile_page_id";s:1:"0";s:9:"user_name";s:12:"profile-1585";s:9:"full_name";s:12:"Sushil Kadam";s:6:"gender";s:1:"0";s:10:"user_image";s:10:"1585%s.jpg";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"7";s:11:"language_id";s:1:"0";s:13:"profile_image";s:59:"http://app.campusknot.com/file/pic/user/1585_100_square.jpg";}i:2;a:10:{s:7:"user_id";s:3:"185";s:15:"profile_page_id";s:1:"0";s:9:"user_name";s:11:"profile-185";s:9:"full_name";s:12:"Perceus Mody";s:6:"gender";s:1:"1";s:10:"user_image";s:9:"185%s.peg";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"6";s:11:"language_id";s:1:"0";s:13:"profile_image";s:58:"http://app.campusknot.com/file/pic/user/185_100_square.peg";}i:3;a:10:{s:7:"user_id";s:3:"196";s:15:"profile_page_id";s:1:"0";s:9:"user_name";s:11:"profile-196";s:9:"full_name";s:11:"Ira Hampton";s:6:"gender";s:1:"1";s:10:"user_image";s:46:"2014/11/24c4a6835e667b67b82cea3666841ac7%s.jpg";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"6";s:11:"language_id";s:1:"0";s:13:"profile_image";s:88:"http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile.png";}i:4;a:10:{s:7:"user_id";s:3:"244";s:15:"profile_page_id";s:1:"0";s:9:"user_name";s:11:"profile-244";s:9:"full_name";s:11:"Hiten Patel";s:6:"gender";s:1:"2";s:10:"user_image";s:9:"244%s.cza";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"7";s:11:"language_id";s:1:"0";s:13:"profile_image";s:58:"http://app.campusknot.com/file/pic/user/244_100_square.cza";}}';
  11. $userListA = unserialize($sA);
  12. // \Kint::dump($sA, $userListA);
  13.  
  14. $sB = 'a:2:{i:0;a:10:{s:7:"user_id";s:3:"244";s:15:"profile_page_id";s:1:"0";s:14:"user_server_id";s:1:"0";s:9:"user_name";s:11:"profile-244";s:9:"full_name";s:11:"Hiten Patel";s:6:"gender";s:1:"2";s:10:"user_image";s:9:"244%s.cza";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"7";s:11:"language_id";s:1:"0";}i:1;a:10:{s:7:"user_id";s:4:"1585";s:15:"profile_page_id";s:1:"0";s:14:"user_server_id";s:1:"0";s:9:"user_name";s:12:"profile-1585";s:9:"full_name";s:12:"Sushil Kadam";s:6:"gender";s:1:"0";s:10:"user_image";s:10:"1585%s.jpg";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"7";s:11:"language_id";s:1:"0";}}';
  15. $userListB = unserialize($sB);
  16. // \Kint::dump($sB, $userListB);
  17.  
  18. /*
  19.  * This version is simple but will be slow for large arrays...
  20.  *
  21.  * Hmm... I may want to do a more efficient version later,  so I will use a
  22.  *        compare function now, so that I can easily change it later.
  23.  */
  24.  
  25. /*
  26.  * Out Array:
  27.  *    1) In $userListA but not in $userListB
  28.  */
  29. $diffAfromB = array();
  30.  
  31. // check the input user list...
  32. foreach ($userListA as $userA) {
  33.  
  34.     // find the userA details in Array B
  35.     if (!matchUserAinB($userA, $userListB)) {
  36.         $diffAfromB[] = $userA;
  37.     }
  38. }
  39.  
  40. // show otput...
  41. var_dump($diffAfromB);
  42.  
  43. exit;
  44.  
  45. // -------------------------------------------------------------------------
  46. /**
  47.  * Check if userA exists in userListB
  48.  *
  49.  * @param array $userA
  50.  * @param array $userListB
  51.  * @return boolean
  52.  */
  53. function matchUserAinB($userA, array $userListB)
  54. {
  55.     $found = false;
  56.  
  57.     foreach ($userListB as $userB) {
  58.         if ($userB['user_id'] === $userA['user_id']) {
  59.             $found = true;
  60.             break;
  61.         }
  62.     }
  63.     return $found;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement