2

I have two arrays

they look like

$a1 = array(
  array('num' => 1, 'name' => 'one'),
  array('num' => 2, 'name' => 'two'),
  array('num' => 3, 'name' => 'three'),
  array('num' => 4, 'name' => 'four'),
  array('num' => 5, 'name' => 'five')
)

$a2 = array(3,4,5,6,7,8);

I want to end up with an array that looks like

$a3 = array(3,4,5);

so basically where $a1[$i]['num'] is in $a2

I know I could do

$a3 = array();

foreach($a1 as $num)
 if(array_search($num['num'], $a2))
   $a3[] = $num['num'];

But that seems like a lot of un-needed iterations.

Is there a better way?


Ah Snap...
I just realized I asked this question the wrong way around, I want to end up with an array that looks like

$a3 array(
  array('num' => 3, 'name' => 'three'),
  array('num' => 4, 'name' => 'four'),
  array('num' => 5, 'name' => 'five')
)

1 Answer 1

2

You could extract the relevant informations (the 'num' items) from $a1 :

$a1_bis = array();
foreach ($a1 as $a) {
    $a1_bis[] = $a['num'];
}

And, then, use array_intersect() to find what is both in $a1_bis and $a2 :

$result = array_intersect($a1_bis, $a2);
var_dump($result);

Which would get you :

array
  2 => int 3
  3 => int 4
  4 => int 5


With this solution :

  • you are going through $a1 only once
  • you trust PHP on using a good algorithm to find the intersection between the two arrays (and/or consider that a function developed in C will probably be faster than any equivalent you could code in pure-PHP)



EDIT after the comment : well, considering the result you want, now, I would go with another approach.

First, I would use array_flip() to flip the $a2 array, to allow faster access to its elements (accessing by key is way faster than finding a value) :

$a2_hash = array_flip($a2); // To speed things up : 
                            // accessing by key is way faster than finding 
                            // an item in an array by value

Then, I would use array_filter() to apply a filter to $a1, keeping the items for which num is in the $a2_hash flipped-array :

$result = array_filter($a1, function ($item) use ($a2_hash) {
    if (isset($a2_hash[ $item['num'] ])) {
        return true;
    }
    return false;
});
var_dump($result);

Note : I used an anonymous function, which only exist with PHP >= 5.3 ; if you are using PHP < 5.3, this code can be re-worked to suppress the closure.

With that, I get the array you want :

array
  2 => 
    array
      'num' => int 3
      'name' => string 'three' (length=5)
  3 => 
    array
      'num' => int 4
      'name' => string 'four' (length=4)
  4 => 
    array
      'num' => int 5
      'name' => string 'five' (length=4)

Note the keys are not corresponding to anything useful -- if you want them removed, just use the array_values() function on that $result :

$final_result = array_values($result);

But that's probably not necessary :-)

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Pascal, Thanks for this, can you please review my edit facepalm
@Hailwook I've edited my answer, with another approach, that makes things easier, considering the result you want :-)

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.