Meta
Meta

Reputation: 1860

PHP compare two arrays for matched key value pairs

Been awake for too many hours and having issues with this one for some reason...

I am trying to compare 2 arrays for matched key=>value pairs and output the matched results.

Array 1: $from['modules']

Array( [8] => Array ( [object_module_id] => 8 [object_module_type] =>
CallLog [object_module_name] => Call Log ) [6] => Array (
[object_module_id] => 6 [object_module_type] => Files
[object_module_name] => Saved Files ) [7] => Array (
[object_module_id] => 7 [object_module_type] => Notes
[object_module_name] => Notes ))

Array 2: $to[0]['modules']

Array( [4] => Array ( [object_module_id] => 4 [object_module_type] =>
CallLog [object_module_name] => Call Log ) [2] => Array (
[object_module_id] => 2 [object_module_type] => Files
[object_module_name] => ) [10] => Array ( [object_module_id] => 10
[object_module_type] => Images [object_module_name] => ) [3] => Array
( [object_module_id] => 3 [object_module_type] => Notes
[object_module_name] => Notes ) [1] => Array ( [object_module_id] => 1
[object_module_type] => Passengers [object_module_name] => Passengers
))

I want to identify modules found in both arrays by both object_module_type && object_module_name and dump the matched results into $module_matches[0] as an array.

Given the above arrays, I know that there should be 3 matches (by type & name)

  1. Array1->8 matches Array2->4
  2. Array1->6 matches Array2->2
  3. Array1->7 matches Array2->3

and I was hoping to get each matches details into an array something like the list above.

EX:

Array( [1] = >Array( ['from'] => ([object_module_id] => 8
[object_module_type] => CallLog [object_module_name] => Call
Log) ['to'] => ([object_module_id] => 4 [object_module_type] => CallLog
[object_module_name] => Call Log)) [2] => (...) [3] => (...))

My current attempt: dump results to $module_matches[0]

$module_matches[0] = array_intersect($to[0]['modules'], $from['modules']);

But the problem is $module_matches[0] just ends up being whatever array was entered into array_intersect() first? In this case $module_matches[0] returns Array2.

Upvotes: 0

Views: 893

Answers (1)

Nash Chandranatha
Nash Chandranatha

Reputation: 26

give this a try..

$matching_results = array();

foreach($from['modules'] as $from_id => $from_module){
    // Parse through each of the $from modules.
    $matches = 0;
    foreach($to[0]['modules'] as $to_id => $to_module){
        // Now we'll compare it to the $to modules...
        foreach($from_module as $from_key => $from_value){
            // So we'll break each $from module apart by key and val...
            if($to_module[$from_key] == $from_module[$from_key] && $from_key != 'object_module_id'){
                // If the array key isn't the ID, and if it matches
                // we'll increase our match counter by 1
                $matches++;
            }
        }
        if($matches == (count($from_module) - 1)){
            // the number of fields that need to match is
            // the total # of fields, minus the ID...
            $matching_results[$from_id] = $from_module;
        }
    }

}

var_dump($matching_results);

Upvotes: 1

Related Questions