Link to home
Start Free TrialLog in
Avatar of stormist
stormist

asked on

PHP- I need to compare two arrays and find the values that match between them

Say i have 2 arrays
array 1 = 0-45, 1-14, 2-16, 3-25, 4-99

array 2 = 0-16 1-15 2-67 3-45 4-88

Is there a simple PHP function to return which values match in another array? In this case a comparative array that returns

0-16
1-45

since both arrays have those values?

Thanks!

Avatar of zreak
zreak

array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

Example 227. array_intersect() example

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
?>

This makes $result have

<?php Array
(
   [a] => green
   [0] => red
) ?>
ASKER CERTIFIED SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stormist

ASKER

Hi all! What would be a way to use that function but reset the keys? first one 0 , 2nd one 1. etc?

Thx!
Use: http://pl2.php.net/manual/en/function.array-values.php
on the resulted array. Resulting array would be indexed by numbers.