phparraysmultidimensional-arrayfilterarray-difference

Filter out rows from a 2d array by correlating a differently-named column from another 2d array


I want to take two multidimensional arrays and compare them, then remove any duplicate records.

The scenario is: The values in array2 have already been assigned to a user's profile. The values in array1 are ALL of the available values that the user can choose from. I want to compare the two so that only the ones not already assigned are given as an option (left in the array)...

$array1 = array(
  [0] => array( [id] => 3 [name] => Eye Colour )
  [1] => array( [id] => 1 [name] => Hair Colour )
  [2] => array( [id] => 5 [name] => Hair Length )
  [3] => array( [id] => 4 [name] => Height )
); 

$array2 = array(
  [0] => array( [attribute_id] => 3 [name] => Eye Colour [active] => 1 )
  [1] => array( [attribute_id] => 5 [name] => Hair Length [active] => 1 ) )
);

PHP's array_diff() function doesn't work with multidimensional arrays.

The result based on the above two arrays should be:

$array1 = array(
  [0] => array( [id] => 1 [name] => Hair Colour )
  [1] => array( [id] => 4 [name] => Height )
);

The [active] field is irrelevant, so I just need it to compare the ID and the Name fields. I realise that the name of the two id fields is different, but it would be a pain to change them as they are database column names.

It needs to completely remove the array, not just the values. I've had issues with previous attempts where it leaves array() in there and then this causes issues when I'm looping through the array generating the fields that the user can choose from.


Solution

  • I don't know how to do it with any built-in PHP function but here's a custom one:

    $array1 = array(
      array( 'id' => 3, 'name' => 'Eye Colour' ),
      array( 'id' => 1, 'name' => 'Hair Colour' ),
      array( 'id' => 5, 'name' => 'Hair Length' ),
      array( 'id' => 4, 'name' => 'Height' ),
    ); 
    
    $array2 = array(
      array( 'attribute_id' => 3, 'name' => 'Eye Colour', 'active' => 1 ),
      array( 'attribute_id' => 5, 'name' => 'Hair Length', 'active' => 1 )
    );
    
    // function to remove duplicates
    function myArrayDiff($array1, $array2) {
        // loop through each item on the first array
        foreach ($array1 as $key => $row) {
            // loop through array 2 and compare
            foreach ($array2 as $key2 => $row2) {
                if ($row['id'] == $row2['attribute_id']) {
                    // if we found a match unset and break out of the loop
                    unset($array1[$key]);
                    break;
                }
            }
        }
    
        return array_values($array1);
    }
    
    $array3 = myArrayDiff($array1, $array2);
    
    print_r($array3);
    
    /* result:
        Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Hair Colour
                )
    
            [1] => Array
                (
                    [id] => 4
                    [name] => Height
                )
    
        )
    */