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);
?>

Run Instance

定义和用法

array_udiff() 函数用于比较两个(或更多个)数组的键值 ,并返回差集。

注释:该函数使用用户自定义函数来比较键值!

该函数比较两个(或更多个)数组的键值,并返回一个差集数组,该数组包括了所有在被比较的数组(array1)中,但是不在任何其他参数数组(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 (array1)中,但是不在任何其他参数数组(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);
?>

Run Instance