Advertisement
rfv123

Q34462068 - quick version - compare_arrays_for_missing_users

Dec 26th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.82 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.  
  4. // include __DIR__ . '/__bootstrap__.php';
  5. /*
  6.  * Convert Print_R back into an array...
  7.  *
  8.  * http://phillihp.com/toolz/php-array-beautifier/
  9.  */
  10.  
  11. $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";}}';
  12. $userListA = unserialize($sA);
  13. // \Kint::dump($sA, $userListA);
  14.  
  15. $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";}}';
  16. $userListB = unserialize($sB);
  17. // \Kint::dump($sB, $userListB);
  18.  
  19. /*
  20.  * This version is  more efficient with large arrays...
  21.  *
  22.  * To speed up the lookups we make an array with 'user_id' as the key...
  23.  */
  24. $KeyedUserListB = array();
  25. foreach ($userListB as $userB) {
  26.    $KeyedUserListB[$userB['user_id']] = true;
  27. }
  28.  
  29. /*
  30.  * Out Array:
  31.  *    1) In $userListA but not in $userListB
  32.  */
  33. $diffAfromB = array();
  34.  
  35. // check the input user list...
  36. foreach ($userListA as $userA) {
  37.  
  38.     // find the userA details in Array B
  39.     if (!matchUserAinB($userA, $KeyedUserListB)) {
  40.         $diffAfromB[] = $userA;
  41.     }
  42. }
  43.  
  44. // show otput...
  45. var_dump($diffAfromB);
  46.  
  47. exit;
  48.  
  49. // -------------------------------------------------------------------------
  50. /**
  51.  * Check if userA exists in userListB
  52.  *
  53.  * @param array $userA
  54.  * @param array $userListB
  55.  * @return boolean
  56.  */
  57. function matchUserAinB($userA, array $userListB)
  58. {
  59.     return isset($userListB[$userA['user_id']]);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement