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.
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;
}