Compare 2-dimensional data sets based on a specified second level value

Viewed 58820

I have an array containing rows of associative data.

$array1 = array(
    array('ITEM' => 1),
    array('ITEM' => 2),
    array('ITEM' => 3),
);

I have a second array, also containing rows of associative data, that I would like to filter using the first array.

$array2 = array(
    array('ITEM' => 2),
    array('ITEM' => 3),
    array('ITEM' => 1),
    array('ITEM' => 4),
);

This feels like a job for array_diff(), but how can I compare the rows exclusively on the deeper ITEM values?

How can I filter the second array and get the following result?

array(3 => array('ITEM' => 4))
9 Answers

Having the same problem but my multidimensional array has various keys unlike your "ITEM" consistently in every array.

Solved it with: $result = array_diff_assoc($array2, $array1);

Reference: PHP: array_diff_assoc

Another solution if( json_encode($array1) == json_encode($array2) ){ ... }

Trust that the maintainers of PHP have optimized array_udiff() to outperform all other techniques which could do the same.

With respect to your scenario, you are seeking a filtering array_diff() that evaluates data within the first level's "value" (the row of data). Within the custom function, the specific column must be isolated for comparison. For a list of all native array_diff() function variations, see this answer.

To use the first array to filter the second array (and output the retained data from the second array), you must write $array2 as the first parameter and $array1 as the second parameter.

array_diff() and array_intersect() functions that leverage (contain u in their function name) expect an integer as their return value. That value is used to preliminary sort the data before actually performing the evaluations -- this is a performance optimization. There may be scenarios where if you only return 0 or 1 (not a three-way comparison), then the results may be unexpected. To ensure a stable result, always provide a comparison function that can return a negative, a positive, and a zero integer.

When comparing integer values, subtraction ($a - $b) will give reliable return values. For greater utility when comparing float values or non-numeric data, you can use the spaceship operator when your PHP version makes it available.

Codes: (Demo)

  • PHP7.4+ (arrow functions)

    var_export(
        array_udiff($array2, $array1, fn($a, $b) => $a['ITEM'] <=> $b['ITEM'])
    );
    
  • PHP7+ (spaceship operator)

    var_export(
        array_udiff(
            $array2,
            $array1,
            function($a, $b) {
                return $a['ITEM'] <=> $b['ITEM'];
            }
        )
    );
    
  • PHP5.3+ (anonymous functions)

    var_export(
        array_udiff(
            $array2,
            $array1,
            function($a, $b) {
                return $a['ITEM'] === $b['ITEM']
                    ? 0
                    : ($a['ITEM'] > $b['ITEM'] ? 1 : -1);
            }
        )
    );
    

Output for all version above:

array (
  3 => 
  array (
    'ITEM' => 4,
  ),
)

When working with object arrays, the technique is the same; only the syntax to access a property is different from accessing an array element ($a['ITEM'] would be $a->ITEM).


For scenarios where the element being isolated from one array does not exist in the other array, you will need to coalesce both $a and $b data to the opposite fallback column because the data from the first array and the second arrays will be represented in both arguments of the callback.

Code: (Demo)

$array1 = array(
    array('ITEM' => 1),
    array('ITEM' => 2),
    array('ITEM' => 3),
);

$array2 = array(
    array('ITEMID' => 2),
    array('ITEMID' => 3),
    array('ITEMID' => 1),
    array('ITEMID' => 4),
);

// PHP7.4+ (arrow functions)
var_export(
    array_udiff(
        $array2,
        $array1,
        fn($a, $b) => ($a['ITEM'] ?? $a['ITEMID']) <=> ($b['ITEM'] ?? $b['ITEMID'])
    )
);

Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

        //Enter your code here, enjoy!

$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
Related