Reputation: 2694
I was under the impression that array_diff evaluates the difference between the values in the two arrays. But somehow it doesn't work...I am guessing nested arrays is the problem here. Clearly array_diff_assoc is not the solution as keys are irrelevant in this case.
I don't even want to make it go nested, just see whether the value (in this case, array) inside are identical or not.
$file_details = array(
array(
"uuid" => "45ebdbaa-380b-483b-80a2-73d7c53088e2",
"filename" => "train_failure.mp3",
),
array("uuid" => "97baa061-4208-4aeb-8136-eb76c0932a3d",
"filename" => "train_work1.mp3"
),
array("uuid" => "ksjdfls6-eb76c0932a3d",
"filename" => "train.mp3"
),
);
$items = array(
array(
"uuid" => "45ebdbaa-380b-483b-80a2-73d7c53088e2",
"filename" => "train_failure.mp3",
),
array(
"uuid" => "1233489eb76c0932a3d",
"filename" => "train.mp3"
),
);
print_r(array_diff($file_details,$items));
This returns an empty array...How should I go about fixing this?
My desired output is
array(
"uuid" => "97baa061-4208-4aeb-8136-eb76c0932a3d",
"filename" => "train_work1.mp3"
),
array(
"uuid" => "ksjdfls6-eb76c0932a3d",
"filename" => "train.mp3"
),
UPDATE -: *I know array_diff doesn't work for 1-d array, I'm just surprised there is no direct php function for doing a comparison on multidimensional arrays.*
Upvotes: 1
Views: 139
Reputation: 17721
From the array_diff() man page:
Note:
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using
array_diff($array1[0], $array2[0]);
Upvotes: 0
Reputation: 37365
Your array items are arrays and cannot be compared as strings (array_diff()
will treat all values as strings - for example, it will try to stringify objects via calling their __toString()
method).
You can use array_udiff() instead:
$rgResult=array_udiff($file_details, $items, function($rgX, $rgY)
{
return $rgX['uuid']<$rgY['uuid']?-1:$rgX['uuid']!=$rgY['uuid'];
});
Upvotes: 2