kemosabe
kemosabe

Reputation: 154

Removing values from an array when the same value exists in another array

I have two arrays that I'm trying to compare. One of the arrays I need to remove the values in it, if the values are present at least once in the first array. Here's what the arrays look like:

array1: {
    1: {
        0: "1"
    },
    1: {
        0: "1"
    },
    24: {
        0: "24"
    },
    24: {
        0: "24"
    },
    24: {
        0: "24"
    },
    24: {
        0: "24"
    },
    26: {
        0: "26"
    }
},

array2: {
    1: {
        0: "blue"
    },
    23: {
        0: "yellow"
    },
    24: {
        0: "red"
    },
    26: {
        0: "green"
    }
},

What I need to do is check array1 keys and if array2 has those same values in the key remove them from array 2. So for this example I should only end up having

array2: {
   23: {
       0: "yellow"
   }
}

I'm having to do this for several different instances of arrays that look similar.

I have tried:

$result = array_diff($array1, $array2);
print_r($result);

and that returns:

{
    25: {
         0: "25"
    }
}
{
    24: {
         0: "24"
    }
}
{
    24: {
         0: "24"
    }
}

While I realize that it's returning those values because there are multiples of them in the first array. I'm wondering how can I get it to ignore the doubles. Also I don't understand why 23 was not returned.

Upvotes: 0

Views: 781

Answers (1)

Bart
Bart

Reputation: 1268

Should work:

<?php 
    foreach($array1 as $a1){
        unset($array2[$a1[0]]);
    }
?>

Upvotes: 2

Related Questions