2

Need to match 2 keys in 2 multidimensional arrays and return matches from the 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 to match 2 key values, in this example only return the matches in array1 that match array2. The key values for example are the keys fruit and name.

In the above example you can see this match should only return $arr1 and $arr4 for array1 since they match $arr[1] in array2. I need to return the matches only for array1.

This is an example, the real case I do not know the array varibale indicator or the amount ( likely a few hundred in each).

Wyck
  • 2,023
  • 4
  • 28
  • 34

3 Answers3

1
$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 as $key => $subarray) {
   if (($subarray['fruit'] == $fruit) && ($subarray['ver'] == $ver))
       $found[$key] = $subarray;
   }
}

After this, $found will be a new array containing copies of all the subarrays which have matching fruit/ver fields.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

This should, very roughly, work. I'd however split into multiple methods in real solution tho.

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

$fruits = array( );
$fruits[ 1 ] = array( 'fruit' => 'apple', 'ver' => '4', 'color' => 'red', 'name' => 'joe' );
$fruits[ 2 ] = array( 'fruit' => 'apple', 'ver' => '4', 'color' => 'red', 'name' => 'jane' );

$keys = array( );

foreach ( $fruits as $fruit ) {
    foreach ( $fruit_list as $list_key => $list_item ) {
        if ( in_array( $fruit[ 'fruit' ], $list_item ) && in_array( $fruit[ 'name' ], $list_item ) ) {
            $keys[ ] = $list_key;
        }
    }
}

var_dump( $keys );

Output:

array(2) {
  [0]=>
  int(1)
  [1]=>
  int(4)
}
Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
0

With PHP 5.3's anonymous functions for convenience, you can write this function:

function compare_arrays($primary, $secondary, $matchKeys) {
    $comparer = function($x, $y) use ($matchKeys) {
        foreach ($matchKeys as $key) {
            if ($x[$key] < $y[$key]) {
                return -1;
            }
            else if($x[$key] > $y[$key]) {
                return 1;
            }
        }

        return 0;
    };

    return array_uintersect($primary, $secondary, $comparer);
}

Which can be used like this:

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

$arr2 = array();
$arr2[] = array('fruit' => 'apple', 'ver' => '4', 'color' => 'red', 'name' =>'joe');
$arr2[] = array('fruit' => 'apple', 'ver' => '4', 'color' => 'red', 'name' =>'jane');

print_r(compare_arrays($arr1, $arr2, array('fruit', 'name')));

The first two arguments are your arrays. The third argument is an array of keys which all have to match for two elements to be considered equal.

When an element in the first array is considered equal to any element of the second array, the element in the first one ($primary) is considered a match. compare_array returns an array containing all these matches.

Jon
  • 428,835
  • 81
  • 738
  • 806