📜  PHP | array_intersect_ukey()函数

📅  最后修改于: 2022-05-13 01:57:40.296000             🧑  作者: Mango

PHP | array_intersect_ukey()函数

array_intersect_ukey()函数是PHP 的内置函数,用于在用户定义函数的帮助下计算两个或多个数组与键的交集,函数返回一个数组。返回的数组是第一个数组,它匹配键并且出现在所有参数中是最终输出。
句法:

array array_intersect_ukey( $array1, $array2, ..., $ukey_intersectFunction )

参数:该函数接受两个或多个数组和一个 $ukey_intersectFunction 参数,如上所述,如下所述:

  • $array1:它是用于与其他数组进行比较的初始数组。它是强制性参数。
  • $array2:与第一个数组键比较的数组。它是强制性参数。
  • $数组。 . . :要与第一个数组键进行比较的数组参数列表。它是可选参数。
  • $ukey_intersectFunction:它是保存用户定义函数的必需参数。定义可调用比较函数的字符串。

返回值:它返回一个数组,其中包含第一个数组的值,该数组的键存在于所有其他参数中。
注意:此函数使用用户定义的函数来比较键。不比较键的值。
下面的例子说明了PHP的 array_intersect_ukey()函数:
程序1:该程序使用两个数组(array1 和array2)和一个用户定义的键比较函数(ukey_intersectFunction)来计算数组的交集。

PHP
 $y)? 1: -1;
}
 
$a1 = array(
    "a" => "Java",
    "b" => "C++",
    "c" => "XML",
    "d" => "Python",
    "p" => "PHP"
);
$a2 = array(
    "a" => "Python",
    "b" => "CSS",
    "h" => "VB",
    "x" => "HTML",
    "p" => "SQL"
);
 
// Print results if the key is matching form both
// array and print first array elements.
$result = array_intersect_ukey($a1, $a2, "ukey_intersectFunction");
print_r($result);
?>


PHP
 $y)? 1: -1;
}
 
// Arrays
$arr1 = array(
    "a" => "A",
    "b" => "B",
    "c" => "D",
    "d" => "E",
    "p" => "F"
);
$arr2 = array(
    "o" => "A",
    "b" => "B",
    "h" => "V",
    "x" => "T",
    "p" => "X"
);
$arr3 = array(
    "a" => "N",
    "b" => "B",
    "h" => "O",
    "x" => "T",
    "p" => "P"
);
$arr4 = array(
    "s" => "W",
    "b" => "B",
    "z" => "D",
    "x" => "T",
    "p" => "P"
);
 
// Print the result if key is matching form both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
   $arr2, $arr3, $arr4, "ukey_intersectFunction");
 
print_r($result);
 
?>


PHP
 $y)? 1: -1;
}
 
// Arrays
$arr1 = array(
    "a" => "Bhopal",
    "b" => "New Delhi",
    "n" => "Madras",
    "o" => "Mumbai",
    "p" => "Finland"
);
$arr2 = array(
    "a" => "Bhubaneswar",
    "b" => "Ranchi",
    "n" => "Kolkatta",
    "o" => "Lucknow",
    "p" => "Allahabad"
);
$arr3 = array(
    "a" => "Rourkela",
    "b" => "Jammu",
    "n" => "Kasmir",
    "o" => "Tripura",
    "p" => "Sillong"
);
 
// Print the result if key is matching form both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
      $arr2, $arr3, "ukey_intersectFunction");
print_r($result);
?>


PHP
 $y)? 1: -1;
}
 
// Arrays
$arr1 = array(
    "a" => "IIT Sillong",
    "b" => "IIT Bhopal",
    "n" => "NIT Bhopal"
);
$arr2 = array(
    "x" => "IIT Bhubaneswar",
    "y" => "IIT Ranchi",
    "z" => "NIT Durgapur"
);
 
 
// Print the result if key is matching form both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
         $arr2, "ukey_intersectFunction");
print_r($result);
?>


PHP
 $y)? 1: -1;
}
 
// Arrays
$arr1 = array(
    "a" => "IIT",
    "b" => "CIT",
    "n" => "NIT"
);
$arr2 = array(
    "x" => "IIT",
    "y" => "CIT",
    "z" => "NIT"
);
 
 
// Print the result if key is matching form both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
         $arr2, "ukey_intersectFunction");
 
print_r($result);
?>


输出:
Array
(
    [a] => Java
    [b] => C++
    [p] => PHP
)

程序2:该程序使用四个数组(array1、array2、array3和array4)和一个用户定义的键比较函数(ukey_intersectFunction)来计算数组的交集。



PHP

 $y)? 1: -1;
}
 
// Arrays
$arr1 = array(
    "a" => "A",
    "b" => "B",
    "c" => "D",
    "d" => "E",
    "p" => "F"
);
$arr2 = array(
    "o" => "A",
    "b" => "B",
    "h" => "V",
    "x" => "T",
    "p" => "X"
);
$arr3 = array(
    "a" => "N",
    "b" => "B",
    "h" => "O",
    "x" => "T",
    "p" => "P"
);
$arr4 = array(
    "s" => "W",
    "b" => "B",
    "z" => "D",
    "x" => "T",
    "p" => "P"
);
 
// Print the result if key is matching form both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
   $arr2, $arr3, $arr4, "ukey_intersectFunction");
 
print_r($result);
 
?>
输出:
Array
(
    [b] => B
    [p] => F
)

程序 3:该程序使用三个数组(array1、array2 和 array3)和一个用户定义的键比较函数来计算数组的交集。

PHP

 $y)? 1: -1;
}
 
// Arrays
$arr1 = array(
    "a" => "Bhopal",
    "b" => "New Delhi",
    "n" => "Madras",
    "o" => "Mumbai",
    "p" => "Finland"
);
$arr2 = array(
    "a" => "Bhubaneswar",
    "b" => "Ranchi",
    "n" => "Kolkatta",
    "o" => "Lucknow",
    "p" => "Allahabad"
);
$arr3 = array(
    "a" => "Rourkela",
    "b" => "Jammu",
    "n" => "Kasmir",
    "o" => "Tripura",
    "p" => "Sillong"
);
 
// Print the result if key is matching form both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
      $arr2, $arr3, "ukey_intersectFunction");
print_r($result);
?>
输出:
Array
(
    [a] => Bhopal
    [b] => New Delhi
    [n] => Madras
    [o] => Mumbai
    [p] => Finland
)

程序4:该程序使用两个数组(array1 和array2),以及一个用户定义的键比较函数(ukey_intersectFunction)来计算数组的交集。两个数组都有不同的键,然后数组返回NULL/Empty Array

PHP

 $y)? 1: -1;
}
 
// Arrays
$arr1 = array(
    "a" => "IIT Sillong",
    "b" => "IIT Bhopal",
    "n" => "NIT Bhopal"
);
$arr2 = array(
    "x" => "IIT Bhubaneswar",
    "y" => "IIT Ranchi",
    "z" => "NIT Durgapur"
);
 
 
// Print the result if key is matching form both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
         $arr2, "ukey_intersectFunction");
print_r($result);
?>
输出:
Array
(
)

程序5:该程序使用两个数组(array1 和array2),并且两个数组在同一索引中具有不同的键但具有相同的值。比较数组并返回NULL/Empty Array

PHP

 $y)? 1: -1;
}
 
// Arrays
$arr1 = array(
    "a" => "IIT",
    "b" => "CIT",
    "n" => "NIT"
);
$arr2 = array(
    "x" => "IIT",
    "y" => "CIT",
    "z" => "NIT"
);
 
 
// Print the result if key is matching form both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
         $arr2, "ukey_intersectFunction");
 
print_r($result);
?>
输出:
Array
(
)

相关文章:

  • PHP | array_diff_key()函数
  • PHP | array_intersect_key()函数
  • PHP | array_intersect()函数
  • PHP | array_intersect_assoc()函数
  • PHP | array_uintersect_assoc()函数

参考文献:http:// PHP.NET /手动/ EN /函数.array-交叉u键。 PHP