PHP array_udiff() 函數
實例
比較兩個數組的鍵值(使用用戶自定義函數比較鍵值),並返回差集:
<?php function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"blue","b"=>"black","e"=>"blue"); $result=array_udiff($a1,$a2,"myfunction"); print_r($result); ?>
定義和用法
array_udiff() 函數用於比較兩個(或更多個)數組的鍵值,並返回差集。
註釋:該函數使用用戶自定義函數來比較鍵值!
該函數比較兩個(或更多個)數組的鍵值,並返回一個差集數組,該數組包括了所有在被比較的數組(array1in, but not in any other parameter array (array2 or array3 etc.) key values.
說明
array_udiff() 函數返回一個數組,該數組包括了所有在被比較數組中,但是不在任何其他參數數組中的值,鍵名保留不變。
array_udiff() 函數與 array_diff() 函數 的行為不同,後者用內部函數進行比較。
數據的比較是用 array_udiff() 函數的 myfunction 進行的。myfunction 函数帶有兩個將會進行比較的參數。如果第一個參數小於第二個參數,則函數返回一個負數,如果兩個參數相等,則要返回 0,如果第一個參數大於第二個,則返回一個正數。
语法
array_udiff(array1,array2,array3...myfunction)
Parameters | Description |
---|---|
array1 | Required. The first array to be compared with the other arrays. |
array2 | Required. The array to be compared with the first array. |
array3,... | Optional. Other arrays to compare with the first array. |
myfunction |
Required. String value, defines the callable comparison function. If the first parameter is less than or equal to or greater than the second parameter, the comparison function must return an integer less than or equal to or greater than 0. |
Technical Details
Return Value: | Returns the difference set array, which contains all the elements in the compared arrays (array1in, but not in any other parameter array (array2 or array3 etc.) key values. |
PHP Version: | 5.1.0+ |
More Examples
Example 1
Compare the key values of three arrays (using a user-defined function to compare key values) and return the difference set:
<?php function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } $a1=array("a"=>"red","b"=>"green","c"=>"blue","yellow"); $a2=array("A"=>"red","b"=>"GREEN","yellow","black"); $a3=array("a"=>"green","b"=>"red","yellow","black"); $result=array_udiff($a1,$a2,$a3,"myfunction"); print_r($result); ?>