当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > array_udiff_assoc() 函数
The array_udiff_assoc() function compares two or more arrays, both in a built in function and in a user-made function, and then returns an array containing the elements from the first array, if the functions allow it. The built in function compares the keys. The user-made function compares the values, and returns a numeric value, a positive number (1) if the returned array should contain this element, and 0, or a negative number (-1), if not.
array_udiff()函数通过比较用户自定义函数和内置函数中的两个或多个数组,找出其中不相同的元素,并返回位于第一个数组中不相同的元素;如果返回值设置为“return 1”,则表示返回的数组中需要包含上述那个位于第一个数组中不同的元素;如果设置为“return 0[或者是一个负数,如:-1]” ,则表示不包含该元素。
array_udiff_assoc(array1,array2,array3...,function) |
Parameter参数 | Description描述 |
---|---|
array1 | Required. The first array is the array that the others will be compared with 必要参数。指定一个参与比较的基准数组 |
array2 | Required. An array to be compared with the first array 必要参数。与数组1[array1]进行比较的数组2[array2] |
array3 | Optional. An array to be compared with the first array 可选参数。与数组1[array1]进行比较的数组3[array3] |
function | Required. The name of the user-made function 必要参数。用户自定义函数的名称 |
Tip: You can compare the first array with one array, or as many as you like.
Note: For comparison, the key is used in the built-in function and the value is used in the user-made function.
注意:这里参与比较的是内置函数中的键和用户自定义函数中的值。
<?php function myfunction($v1,$v2) { if ($v1===$v2) { return 0; } return 1; } $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse"); $a2=array("a"=>"Cat","b"=>"Horse","c"=>"Dog"); print_r(array_udiff_assoc($a1,$a2,"myfunction")); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Array ( [b] Dog [c] => Horse ) |