array_diff to compare two associative arrays
I believe you want to use array_diff_assoc http://www.php.net/manual/en/function.array-diff-assoc.php
I believe you want to use array_diff_assoc http://www.php.net/manual/en/function.array-diff-assoc.php
Two values from key => value pairs are considered equal only if (string) $elem1 === (string) $elem2 . In other words a strict check takes place so the string representations must be the same. http://php.net/manual/en/function.array-diff-assoc.php The (string) value of any array is “Array”. Thus, your call to array_diff_assoc is effectively comparing these two things: Array … Read more
They both do the same, but udiff-assoc compares the DATA with the user supplied function, while diff-uassoc compares the INDEX with the user supplied function. As an answer to @lonsesomeday : as indicated by the ‘u’, diff_assoc will use internal functions for all comparisons, and udiff_uassoc uses provided callbacks for index and data comparison. http://www.php.net/manual/en/function.array-diff-uassoc.php … Read more
Convert arrays to a format, where array index is the sight_id: $b1 =array(); foreach($a1 as $x) $b1[$x[‘sight_id’]] = $x[‘location’]; $b2 =array(); foreach($a2 as $x) $b2[$x[‘sight_id’]] = $x[‘location’]; Calculate the differences and intersection: $c_intersect = array_intersect_key($b1,$b2); $c_1 = array_diff_key($b1,$b2); $c_2 = array_diff_key($b2,$b1); Convert arrays back to your format: $intersect_array = array(); foreach($c_intersect as $i=>$v) $intersect_array[] = … Read more
I know this is an old question, but I thought I would share this little trick. var diff = $(old_array).not(new_array).get(); diff now contains what was in old_array that is not in new_array
To check multi-deminsions try something like this: $pageWithNoChildren = array_map(‘unserialize’, array_diff(array_map(‘serialize’, $pageids), array_map(‘serialize’, $parentpage))); array_map() runs each sub-array of the main arrays through serialize() which converts each sub-array into a string representation of that sub-array the main arrays now have values that are not arrays but string representations of the sub-arrays array_diff() now has a … Read more
You can define a custom comparison function using array_udiff(). function udiffCompare($a, $b) { return $a[‘ITEM’] – $b[‘ITEM’]; } $arrdiff = array_udiff($arr2, $arr1, ‘udiffCompare’); print_r($arrdiff); Output: Array ( [3] => Array ( [ITEM] => 4 ) ) This uses and preserves the arrays’ existing structure, which I assume you want.
How to get the difference between two arrays in JavaScript?