I have two nested arrays and need to find the difference between the reference array and the data array. I am using array_dif_assoc function, and am unable to get the right difference, I am not sure why I am unable to get it. Could somebody help me out if I am doing some mistake or if I have to do it recursively;
$allCoursesAvailable = array(array('id'=>0,'name'=>'Select-One'), array('id'=>1,'name'=>'course1'),array('id'=>1,'name'=>'course2'),array('id'=>3,'name'=>'course3'));
$allCoursesforUser = array(array('id'=>0,'name'=>'Select-One'), array('id'=>1,'name'=>'course1'),array('id'=>4,'name'=>'course4'),array('id'=>5,'name'=>'course5'),array('id'=>6,'name'=>'course4'));
echo '<pre>';print_r(array_diff_assoc($allCoursesAvailable,$allCoursesforUser));
I am getting an empty array with this. When I use array_diff_assoc(), I should have got the arrays carrying course2 and course3, as they are not part of the 2nd array.
Am I missing some logic on the PHP end?
You could always start by reading the PHP manual.
For
array_diff_assoc(on http://php.net/manual/en/function.array-diff-assoc.php) it is said thatthis function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, array_diff_assoc($array1[0], $array2[0]);.Provided solution in user comments (this does work):