Compute array intersection, preserve header order?

Viewed 854

I have two arrays:

<?php
$row = array(
  'useless' => 42,
  'useful' => 'orange',
  'useful_also' => 'apples',
  'useless_too' => 'banana',
  'useful_too' => 'apples');
$header = array('useful_too', 'useful', 'useful_also');

How can I get the following array?

   <?php
   $result = array(
      'useful_too' => 'apples',
      'useful'     => 'orange',
      'useful_also' => 'apples'
   );

I was using array_intersect_key, but it would not use the second array order, but the first. I guess I'll use a simple foreach for the moment, like this:

<?php
$result = array();
foreach ($header as $key) {
    $result[$key] = $row[$key];
}

but I was wondering if there was a solution without a foreach.

2 Answers
Related