2

if anyone could please help me here I would be eternally grateful as I've spent about 2 full days now trying to get this to work. 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, and I've had a good search around but can't seem to find anything that works for me!

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.

Please help. I will buy you many beers! :)

Thanks, Steve

SigmaSteve
  • 664
  • 6
  • 25

1 Answers1

2

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
            )

    )
*/
chrislondon
  • 12,487
  • 5
  • 26
  • 65
  • Hi Chris, thank you very much for taking the time to write this - it works perfectly and is exactly what I needed. I'm not sure if there's a more efficient way of doing it? I'm just aware that it's going to be cycling through $array2 multiple times, which could get a bit laggy when there's a large user base. Still, I am very grateful for your time and expertise. Many thanks, Steve – SigmaSteve Jun 09 '13 at 17:02
  • Thank you! Its 1am here and thousands of lines of code later, your solution is the only one that actually does what its supposed to and have good commenting! – Drmzindec Oct 22 '13 at 23:12