|
array_udiff
Computes the difference of arrays by using a callback function for data comparison
(PHP 5)
array_udiff() returns an array containing all the values of array1 that are not present in any of the other arguments. Note that keys are preserved. For the comparison of the data data_compare_func is used. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. This is unlike array_diff() which uses an internal function for comparing the data. Example 282. array_udiff() example<?php The above example will output: Array
Note:
Please note that this function only checks one dimension of a n-dimensional
array. Of course you can check deeper dimensions by using
See also array_diff(), array_diff_assoc(), array_diff_uassoc(), array_udiff_assoc(), array_udiff_uassoc(), array_intersect(), array_intersect_assoc(), array_uintersect(), array_uintersect_assoc() and array_uintersect_uassoc(). Code Examples / Notes » array_udiffdmhouse
Very easy way of achieving a case-insensitive version of array_diff (or indeed array_diff_assoc, array_intersect or any of these types of functions which have a similar function that takes a callback function as one of their parameters): array_udiff($array1, $array2, 'strcasecmp'); This works because strcasecmp() compares two strings case-insensitively, as compared to the array_diff() which compares two strings by using the == operator, which is case-sensitive. aidan
This functionality is now implemented in the PEAR package PHP_Compat. More information about using this function without upgrading your version of PHP can be found on the below link: http://pear.php.net/package/PHP_Compat colin
I think the example given here using classes is convoluting things too much to demonstrate what this function does. array_udiff() will walk through array_values($a) and array_values($b) and compare each value by using the passed in callback function. To put it another way, array_udiff() compares $a[0] to $b[0], $b[1], $b[2], and $b[3] using the provided callback function. If the callback returns zero for any of the comparisons then $a[0] will not be in the returned array from array_udiff(). It then compares $a[1] to $b[0], $b[1], $b[2], and $b[3]. Then, finally, $a[2] to $b[0], $b[1], $b[2], and $b[3]. For example, compare_ids($a[0], $b[0]) === -5 while compare_ids($a[1], $b[1]) === 0. Therefore, $a[1] is not returned from array_udiff() since it is present in $b. <? $a = array( array( 'id' => 10, 'name' => 'John', 'color' => 'red', ), array( 'id' => 20, 'name' => 'Elise', 'color' => 'blue', ), array( 'id' => 30, 'name' => 'Mark', 'color' => 'red', ), ); $b = array( array( 'id' => 15, 'name' => 'Nancy', 'color' => 'black', ), array( 'id' => 20, 'name' => 'Elise', 'color' => 'blue', ), array( 'id' => 30, 'name' => 'Mark', 'color' => 'red', ), array( 'id' => 40, 'name' => 'John', 'color' => 'orange', ), ); function compare_ids($a, $b) { return ($a['id'] - $b['id']); } function compare_names($a, $b) { return strcmp($a['name'], $b['name']); } $ret = array_udiff($a, $b, 'compare_ids'); var_dump($ret); $ret = array_udiff($b, $a, 'compare_ids'); var_dump($ret); $ret = array_udiff($a, $b, 'compare_names'); var_dump($ret); ?> Which returns the following. In the first return we see that $b has no entry in it with an id of 10. <? array(1) { [0]=> array(3) { ["id"]=> int(10) ["name"]=> string(4) "John" ["color"]=> string(3) "red" } } ?> In the second return we see that $a has no entry in it with an id of 15 or 40. <? array(2) { [0]=> array(3) { ["id"]=> int(15) ["name"]=> string(5) "Nancy" ["color"]=> string(5) "black" } [3]=> array(3) { ["id"]=> int(40) ["name"]=> string(4) "John" ["color"]=> string(6) "orange" } } ?> In third return we see that all names in $a are in $b (even though the entry in $b whose name is 'John' is different, the anonymous function is only comparing names). <? array(0) { } ?> |
Change Language![]() array_change_key_case array_chunk array_combine array_count_values array_diff_assoc array_diff_key array_diff_uassoc array_diff_ukey array_diff array_fill_keys array_fill array_filter array_flip array_intersect_assoc array_intersect_key array_intersect_uassoc array_intersect_ukey array_intersect array_key_exists array_keys array_map array_merge_recursive array_merge array_multisort array_pad array_pop array_product array_push array_rand array_reduce array_reverse array_search array_shift array_slice array_splice array_sum array_udiff_assoc array_udiff_uassoc array_udiff array_uintersect_assoc array_uintersect_uassoc array_uintersect array_unique array_unshift array_values array_walk_recursive array_walk array arsort asort compact count current each end extract in_array key krsort ksort list natcasesort natsort next pos prev range reset rsort shuffle sizeof sort uasort uksort usort |