php - Compare 2 arrays to match 2 keys -


need match 2 keys in 2 multidimensional arrays , return matches first array if found.

array1 =>  $arr[1] = array('fruit' => 'apple', 'ver' => '1', 'color' => 'blue', 'name' =>'joe'); $arr[2] = array('fruit' => 'peach', 'ver' => '2', 'color' => 'red', 'name' =>'jane'); $arr[3] = array('fruit' => 'apple', 'ver' => '1', 'color' => 'red', 'name' =>'jack'); $arr[4] = array('fruit' => 'apple', 'ver' => '4', 'color' => 'grey', 'name' =>'joe');    array2 =>  $arr[1] = array('fruit' => 'apple', 'ver' => '4', 'color' => 'red', 'name' =>'joe'); $arr[2] = array('fruit' => 'apple', 'ver' => '4', 'color' => 'red', 'name' =>'jane'); 

i need match 2 key values, in example return matches in array1 match array2. key values example keys fruit , name.

in above example can see match should return $arr1 , $arr4 array1 since match $arr[1] in array2. need return matches array1.

this example, real case not know array varibale indicator or amount ( few hundred in each).

$array = (     0 => array('fruit' => 'apple' etc....     etc... );  $find_these = array('fruit' => 'apple', 'ver' => 4, 'color' => red, etc...);  $fruit = $find_these['apple']; $ver = $find_these['ver'];  $found = array();  foreach($array $key => $subarray) {    if (($subarray['fruit'] == $fruit) && ($subarray['ver'] == $ver))        $found[$key] = $subarray;    } } 

after this, $found new array containing copies of subarrays have matching fruit/ver fields.


Comments