I have two multidimensional arrays. the first one is:
我有两个多维数组。第一个是:
Array
(
[0] => Array
(
[username] => J1
[fullname] => John1
)
[1] => Array
(
[username] => K1
[fullname] => Kate
)
[2] => Array
(
[username] => P1
[fullname] => Peter
)
[3] => Array
(
[username] => M1
[fullname] => Mary
)
)
and the second one is:
Array
(
[0] => Array
(
[username] => J1
[fullname] => John33
)
[1] => Array
(
[username] => L1
[fullname] => Lucas
)
[2] => Array
(
[username] => P1
[fullname] => Paul
)
)
I want to compare the two arrays using only their username option in order to get the elements of first array which are not included in second array (comparing only the usernames).
我想仅使用他们的用户名选项来比较两个数组,以获得第一个数组的元素,这些元素不包含在第二个数组中(仅比较用户名)。
so i want a new array like the following:
所以我想要一个像以下一样的新数组:
Array
(
[0] => Array
(
[username] => K1
[fullname] => Kate
)
[1] => Array
(
[username] => M1
[fullname] => Mary
)
)
Any help?? thanks a lot!!!
任何帮助?非常感谢!!!
3 个解决方案
#1
You could do something like this in PHP >= 5.5.0:
您可以在PHP> = 5.5.0中执行类似的操作:
$result = array_diff_key(array_column($array1, null, 'username'),
array_column($array2, null, 'username')
);
#2
Try to loop through one array and compare like this:
尝试循环遍历一个数组并进行比较,如下所示:
$array_one = Array
(
[0] => Array
(
[username] => J1
[fullname] => John1
)
[1] => Array
(
[username] => K1
[fullname] => Kate
)
[2] => Array
(
[username] => P1
[fullname] => Peter
)
[3] => Array
(
[username] => M1
[fullname] => Mary
)
);
$array_two = Array
(
[0] => Array
(
[username] => J1
[fullname] => John33
)
[1] => Array
(
[username] => L1
[fullname] => Lucas
)
[2] => Array
(
[username] => P1
[fullname] => Paul
)
);
foreach($array_one as $key => $value) {
if($value['username'] == $array_two[$key]['username'])
// do something
else
// do something
}
#3
$array1 = array(
"0" => Array(
"username" => "J1",
"fullname" => "John1"
),
"1" => Array(
"username" => "K1",
"fullname" => "Kate"
),
"2" => Array(
"username" => "P1",
"fullname" => "Peter"
),
"3" => Array(
"username" => "M1",
"fullname" => "Mary"
)
);
$array2 = array(
"0" => Array(
"username" => "J1",
"fullname" => "John33"
),
"1" => Array(
"username" => "L1",
"fullname" => "Lucas"
),
"2" => Array(
"username" => "P1",
"fullname" => "Paul"
)
);
$newarr = array();
foreach( $array1 as $key => $val ) {
if($val['username'] != $array2[$key]['username']){
$newarr[] = $val;
}
}
echo '<pre>';print_r($newarr);echo '</pre>';
Output :
Array
(
[0] => Array
(
[username] => K1
[fullname] => Kate
)
[1] => Array
(
[username] => M1
[fullname] => Mary
)
)
#1
You could do something like this in PHP >= 5.5.0:
您可以在PHP> = 5.5.0中执行类似的操作:
$result = array_diff_key(array_column($array1, null, 'username'),
array_column($array2, null, 'username')
);
#2
Try to loop through one array and compare like this:
尝试循环遍历一个数组并进行比较,如下所示:
$array_one = Array
(
[0] => Array
(
[username] => J1
[fullname] => John1
)
[1] => Array
(
[username] => K1
[fullname] => Kate
)
[2] => Array
(
[username] => P1
[fullname] => Peter
)
[3] => Array
(
[username] => M1
[fullname] => Mary
)
);
$array_two = Array
(
[0] => Array
(
[username] => J1
[fullname] => John33
)
[1] => Array
(
[username] => L1
[fullname] => Lucas
)
[2] => Array
(
[username] => P1
[fullname] => Paul
)
);
foreach($array_one as $key => $value) {
if($value['username'] == $array_two[$key]['username'])
// do something
else
// do something
}
#3
$array1 = array(
"0" => Array(
"username" => "J1",
"fullname" => "John1"
),
"1" => Array(
"username" => "K1",
"fullname" => "Kate"
),
"2" => Array(
"username" => "P1",
"fullname" => "Peter"
),
"3" => Array(
"username" => "M1",
"fullname" => "Mary"
)
);
$array2 = array(
"0" => Array(
"username" => "J1",
"fullname" => "John33"
),
"1" => Array(
"username" => "L1",
"fullname" => "Lucas"
),
"2" => Array(
"username" => "P1",
"fullname" => "Paul"
)
);
$newarr = array();
foreach( $array1 as $key => $val ) {
if($val['username'] != $array2[$key]['username']){
$newarr[] = $val;
}
}
echo '<pre>';print_r($newarr);echo '</pre>';
Output :
Array
(
[0] => Array
(
[username] => K1
[fullname] => Kate
)
[1] => Array
(
[username] => M1
[fullname] => Mary
)
)