将array_intersect与多维数组和不同的键一起使用
Having this array ($firstArray
):
array(3) {
[0]=>
array(2) {
["Name"]=>
string(3) "foo"
["id"]=>
string(4) "1064"
}
[1]=>
array(2) {
["Name"]=>
string(3) "boo"
["id"]=>
string(4) "1070"
}
[2]=>
array(2) {
["Name"]=>
string(3) "bar"
["id"]=>
string(4) "1081"
}
And this one ($secondArray
):
array(2) {
[0]=>
string(4) "1064"
[1]=>
string(4) "1081"
}
How can I use array_intersect
on these inner arrays?
I tried array_intersect($firstArray, $secondArray);
which is not working.
My desired output would be:
array(2) {
[0]=>
array(2) {
["Name"]=>
string(3) "foo"
["id"]=>
string(4) "1064"
}
[1]=>
array(2) {
["Name"]=>
string(3) "bar"
["id"]=>
string(4) "1081"
}
PS: I'm using PHP 5.2 (I cant update the version as it's not my own machine)
Thanks in advance.
拥有此数组( 这一个( $ secondArray code>): p>
如何在这些内部数组上使用 我尝试了 我想要的输出是 : p>
PS:我使用的是PHP 5.2(我无法更新版本,因为它不是我自己的机器) p>
提前致谢。 p>
$ firstArray code>): p>
array(3){
[0] =>
array(2){
[“Name”] =>
string(3)“foo”
[“id” ] =>
string(4)“1064”
}
[1] =>
array(2){
[“Name”] =>
string(3)“boo”
[“id”] =>
string(4)“1070”
}
[2] =>
array(2){
[“Name”] =>
string (3)“bar”
[“id”] =>
string(4)“1081”
}
code> pre>
array(2){
[0] =>
string(4)“1064”
[1 ] =>
string(4)“1081”
}
code> pre>
array_intersect code>? p>
array_intersect($ firstArray,$ secondArray); code>无效。 p>
array(2){
[0] =>
array(2){
[“Name”] =>
string(3 )“foo”
[“id”] =>
string(4)“1064”
}
[1] =>
array(2){\ n [“名称”] =>
字符串(3)“bar”
[“id”] =>
string(4)“1081”
}
code> pre>
You can't use array_intersect
on two arrays with different structures. To achieve your goal, you have to loop through your first array and check if the id
value is in the second array, as such :
$outputArray = array();
foreach ($firstArray as $value) {
if (in_array($value['id'], $secondArray))
$outputArray[] = $value;
}