使用 php 中的特定键比较多维数组
Comparing multidimensional arrays using specific key in php
我有两个多维数组。第一个是:
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
)
)
我想仅使用用户名选项比较两个数组,以获得第一个数组中未包含在第二个数组中的元素(仅比较用户名)。
所以我想要一个如下所示的新数组:
Array
(
[0] => Array
(
[username] => K1
[fullname] => Kate
)
[1] => Array
(
[username] => M1
[fullname] => Mary
)
)
有什么帮助吗??非常感谢!!!
尝试遍历一个数组并像这样比较:
$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
}
$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>';
输出:
Array
(
[0] => Array
(
[username] => K1
[fullname] => Kate
)
[1] => Array
(
[username] => M1
[fullname] => Mary
)
)
你可以在 PHP >= 5.5.0:
中做这样的事情
$result = array_diff_key(array_column($array1, null, 'username'),
array_column($array2, null, 'username')
);
我有两个多维数组。第一个是:
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
)
)
我想仅使用用户名选项比较两个数组,以获得第一个数组中未包含在第二个数组中的元素(仅比较用户名)。
所以我想要一个如下所示的新数组:
Array
(
[0] => Array
(
[username] => K1
[fullname] => Kate
)
[1] => Array
(
[username] => M1
[fullname] => Mary
)
)
有什么帮助吗??非常感谢!!!
尝试遍历一个数组并像这样比较:
$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
}
$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>';
输出:
Array
(
[0] => Array
(
[username] => K1
[fullname] => Kate
)
[1] => Array
(
[username] => M1
[fullname] => Mary
)
)
你可以在 PHP >= 5.5.0:
中做这样的事情$result = array_diff_key(array_column($array1, null, 'username'),
array_column($array2, null, 'username')
);