-2

I have array1

array1(
  'orange' => 'orange'
  'banana' => 'banana'
);

and array2

array2(
    0 => 'apple'
    1 => 'watermellon'
    2 => 'orange'
    3 => 'potatoes'
    4 => 'lemon'
    5 => 'banana'
)

and i want to take as reply

array3(
    2 => 'orange'
    5 => 'banana'
)

I need a real help here!!

Karmen
  • 238
  • 2
  • 15
  • It's not clear to me what should happen to create the output array 3. Do you want to filter array2 based on the values from array1 or something else? Please explain some more. – Peter May 18 '15 at 12:45
  • http://php.net/manual/en/function.array-intersect.php maybe? – AbraCadaver May 18 '15 at 12:48

2 Answers2

3

Just use array_intersect():

$array3 = array_intersect($array2, $array1);
Ulver
  • 905
  • 8
  • 13
0

Probably not the nicer way to do that, but this do the job:

$array3 = array();
foreach($array1 as $value) {
    if(in_array($value, $array2)) {
        $array3[array_search($value, $array2)] = $value;
    }
}
vard
  • 4,057
  • 2
  • 26
  • 46