I have two arrays of objects.
What I want to do it to find differences based on a certain attribute of the object IF another attribute is equal.
A cut down version of my code is (viewable here);
<?php
echo "Testing\n";
$aud1 = new stdClass();
$aud1->code = 'Z1';
$aud1->sub_list = '2, 3, 1';
$aud2 = new stdClass();
$aud2->code = 'Z2';
$aud2->sub_list = '2, 4, 1';
$aud3 = new stdClass();
$aud3->code = 'Z3';
$aud3->sub_list = '2, 3, 1';
$aud4 = new stdClass();
$aud4->code = 'Z2';
$aud4->sub_list = '2, 3, 1';
$array1 = array(
$aud1,
$aud3
);
$array2 = array(
$aud2,
$aud4
);
echo "\nsample A\n";
print_r($array1);
echo "\nsample B\n";
print_r($array2);
$arrdiff1 = array_values(array_udiff($array1, $array2, 'myDiff'));
echo "\nIn A but not in B\n";
print_r($arrdiff1);
$arrdiff2 = array_values(array_udiff($array2, $array1, 'myDiff'));
echo "\nIn B but not in A\n";
print_r($arrdiff2);
function myDiff($a, $b) {
if ($a->code == $b->code) {
return strcmp($a->sub_list, $b->sub_list);
} else {
return 0;
}
}
?>
I am expecting to see a difference where A is Z2 2,3,1 and B is Z2 2,4,1
Why is this not showing?
UPDATE
As @pilsetnieks says, I got my elements mixed up - not enough coffee.
BUt I am confused as to why doesn't show up in $arraDiff1.
Updated code here.