I've two arrays $a
and $b
containing 'user details' in arrays.
Following is the structure of it(i.e. output of print_r()
)
print_r($a);
Array
(
[0] => Array
(
[user_id] => 109
[profile_page_id] => 0
[user_name] => profile-109
[full_name] => Hiten Patel
[gender] => 0
[user_image] =>
[is_invisible] => 0
[user_group_id] => 6
[language_id] =>
[profile_image] => http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile_100.png
)
[1] => Array
(
[user_id] => 1585
[profile_page_id] => 0
[user_name] => profile-1585
[full_name] => Sushil Kadam
[gender] => 0
[user_image] => 1585%s.jpg
[is_invisible] => 0
[user_group_id] => 7
[language_id] =>
[profile_image] => http://app.campusknot.com/file/pic/user/1585_100_square.jpg
)
[2] => Array
(
[user_id] => 185
[profile_page_id] => 0
[user_name] => profile-185
[full_name] => Perceus Mody
[gender] => 1
[user_image] => 185%s.peg
[is_invisible] => 0
[user_group_id] => 6
[language_id] =>
[profile_image] => http://app.campusknot.com/file/pic/user/185_100_square.peg
)
[3] => Array
(
[user_id] => 196
[profile_page_id] => 0
[user_name] => profile-196
[full_name] => Ira Hampton
[gender] => 1
[user_image] => 2014/11/24c4a6835e667b67b82cea3666841ac7%s.jpg
[is_invisible] => 0
[user_group_id] => 6
[language_id] =>
[profile_image] => http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile.png
)
[4] => Array
(
[user_id] => 244
[profile_page_id] => 0
[user_name] => profile-244
[full_name] => Hiten Patel
[gender] => 2
[user_image] => 244%s.cza
[is_invisible] => 0
[user_group_id] => 7
[language_id] =>
[profile_image] => http://app.campusknot.com/file/pic/user/244_100_square.cza
)
)
The second array is as follows :
print_r($b);
Array
(
[0] => Array
(
[user_id] => 244
[profile_page_id] => 0
[user_server_id] => 0
[user_name] => profile-244
[full_name] => Hiten Patel
[gender] => 2
[user_image] => 244%s.cza
[is_invisible] => 0
[user_group_id] => 7
[language_id] =>
)
[1] => Array
(
[user_id] => 1585
[profile_page_id] => 0
[user_server_id] => 0
[user_name] => profile-1585
[full_name] => Sushil Kadam
[gender] => 0
[user_image] => 1585%s.jpg
[is_invisible] => 0
[user_group_id] => 7
[language_id] =>
)
)
Now I want to compare the two arrays $a and $b
and finally I want refined array $a
which will not contain common array elements as follows(the array keys should start from 0 and keep on increasing by 1).
print_r($a); //after refinement, the desired output array
Array
(
[0] => Array
(
[user_id] => 109
[profile_page_id] => 0
[user_name] => profile-109
[full_name] => Hiten Patel
[gender] => 0
[user_image] =>
[is_invisible] => 0
[user_group_id] => 6
[language_id] =>
[profile_image] => http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile_100.png
)
[1] => Array
(
[user_id] => 185
[profile_page_id] => 0
[user_name] => profile-185
[full_name] => Perceus Mody
[gender] => 1
[user_image] => 185%s.peg
[is_invisible] => 0
[user_group_id] => 6
[language_id] =>
[profile_image] => http://app.campusknot.com/file/pic/user/185_100_square.peg
)
[2] => Array
(
[user_id] => 196
[profile_page_id] => 0
[user_name] => profile-196
[full_name] => Ira Hampton
[gender] => 1
[user_image] => 2014/11/24c4a6835e667b67b82cea3666841ac7%s.jpg
[is_invisible] => 0
[user_group_id] => 6
[language_id] =>
[profile_image] => http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile.png
)
)
For achieving this desired output result I tried following code bu couldn't get the desired refined output result as I expected.
//My attempted code
$a = $result=array_diff($a,$b);
I also tried array_diff_assoc()
function but that also didn't give me the desired output result.
Can someone please guide me in this regard in order to bring the desired output result?
Return an array containing the difference between two supplied user lists.
1) Given two arrays:
Generate an array the contains the users that are in $userListA but not in $userListB.
working example - eval.in; Source code - pastebin
There is a more efficient version that will be rather faster in large arrays. It uses a array 'keyed' on user_id
so can check for missing without scanning all the array. pastebin - compare array for missing user - quick
the code:
/*
* This version is simple but will be slow for large arrays...
*
* Hmm... I may want to do a more efficient version later, so I will use a
* compare function now, so that I can easily change it later.
*/
/*
* Out Array:
* 1) In $userListA but not in $userListB
*/
$diffAfromB = array();
// check the input user list...
foreach ($userListA as $userA) {
// find the userA details in Array B
if (!matchUserAinB($userA, $userListB)) {
$diffAfromB[] = $userA;
}
}
// show otput...
var_dump($diffAfromB);
exit;
// -------------------------------------------------------------------------
/**
* Check if userA exists in userListB
*
* @param array $userA
* @param array $userListB
* @return boolean
*/
function matchUserAinB($userA, array $userListB)
{
$found = false;
foreach ($userListB as $userB) {
if ($userB['user_id'] === $userA['user_id']) {
$found = true;
break;
}
}
return $found;
}
The output:
array
0 =>
array
'user_id' => string '109' (length=3)
'profile_page_id' => string '0' (length=1)
'user_name' => string 'profile-109' (length=11)
'full_name' => string 'Hiten Patel' (length=11)
'gender' => string '0' (length=1)
'user_image' => string '\"\"' (length=4)
'is_invisible' => string '0' (length=1)
'user_group_id' => string '6' (length=1)
'language_id' => string '0' (length=1)
'profile_image' => string 'http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile_100.png' (length=92)
1 =>
array
'user_id' => string '185' (length=3)
'profile_page_id' => string '0' (length=1)
'user_name' => string 'profile-185' (length=11)
'full_name' => string 'Perceus Mody' (length=12)
'gender' => string '1' (length=1)
'user_image' => string '185%s.peg' (length=9)
'is_invisible' => string '0' (length=1)
'user_group_id' => string '6' (length=1)
'language_id' => string '0' (length=1)
'profile_image' => string 'http://app.campusknot.com/file/pic/user/185_100_square.peg' (length=58)
2 =>
array
'user_id' => string '196' (length=3)
'profile_page_id' => string '0' (length=1)
'user_name' => string 'profile-196' (length=11)
'full_name' => string 'Ira Hampton' (length=11)
'gender' => string '1' (length=1)
'user_image' => string '2014/11/24c4a6835e667b67b82cea3666841ac7%s.jpg' (length=46)
'is_invisible' => string '0' (length=1)
'user_group_id' => string '6' (length=1)
'language_id' => string '0' (length=1)
'profile_image' => string 'http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile.png' (length=88)
The test data:
$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";}}';
$userListA = unserialize($sA);
$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";}}';
$userListB = unserialize($sB);