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.
array_diff()would work for an ordinary problem, but it looks like you are dealing with arrays containing arrays of 1 element, instead of simply having an array with multiple elements. I don't know how you managed to get yourself in this situation but I'm pretty sure you should look this up first instead of writing nasty extensive code just to deal with this complication.array_diff()wont work because you are not comparing the elements, you are comparing arrays to arrays because you have the actual information nested in a second level. Comparing arrays to arrays directly doesn't work in PHP and thereforearray_diff()is useless in this case, however the real question is, why the hell you have it that way in the first place?