Hàm array_intersect_ukey () trong PHP
Thí dụ
So sánh các khóa của hai mảng (sử dụng chức năng so sánh khóa do người dùng xác định) và trả về các kết quả phù hợp:
<?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_intersect_ukey($a1,$a2,"myfunction");
print_r($result);
?>
Định nghĩa và Cách sử dụng
Hàm array_intersect_ukey () so sánh các khóa của hai (hoặc nhiều) mảng và trả về các giá trị khớp.
Lưu ý: Chức năng này sử dụng một chức năng do người dùng xác định để so sánh các phím!
Hàm này so sánh các khóa của hai hoặc nhiều mảng và trả về một mảng có chứa các mục từ array1 có trong array2 , array3 , v.v.
Cú pháp
array_intersect_ukey(array1, array2, array3, ..., myfunction)
Giá trị tham số
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 |
array3,... | Optional. An array to be compared with the first array |
myfunction | Required. A string that define a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument |
Chi tiết kỹ thuật
Giá trị trả lại: | Trả về một mảng chứa các mục từ array1 có trong tất cả các mảng khác |
---|---|
Phiên bản PHP: | 5.1.0+ |
Các ví dụ khác
Thí dụ
So sánh các khóa của ba mảng (sử dụng một hàm do người dùng xác định để so sánh các khóa) và trả về các kết quả phù hợp:
<?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"=>"black","b"=>"yellow","d"=>"brown");
$a3=array("e"=>"purple","f"=>"white","a"=>"gold");
$result=array_intersect_ukey($a1,$a2,$a3,"myfunction");
print_r($result);
?>
❮ Tham chiếu mảng PHP