0

I have two arrays of the same length containing some values.

$a = array("a","b","x","x");
$b = array("f","g","g","h");

Now I want to get the values from $b at the index postions from where $a is x.

 $ids = array_keys($a, 'x');
 $res = ???($b,$ids);
 print_r($res);

So what function will give me an Array containing g and h. Or is there even a more elegent (e.g. not using array_keys()) to do this?

guri
  • 662
  • 2
  • 8
  • 26
jakob-r
  • 6,824
  • 3
  • 29
  • 47

1 Answers1

1
$needle = 'x';
$res    = array();
foreach($a as $key => $value) {
    if ($value == $needle) {
        $res[] = $b[$key];
    }
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345