0

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.

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.