Hàm array_intersect_uassoc () trong PHP
Thí dụ
So sánh các khóa và giá trị của hai mảng và trả về các kết quả phù hợp (sử dụng hàm so sánh khóa do người dùng xác định):
<?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("d"=>"red","b"=>"green","e"=>"blue");
$result=array_intersect_uassoc($a1,$a2,"myfunction");
print_r($result);
?>
Định nghĩa và Cách sử dụng
Hàm array_intersect_uassoc () so sánh các khóa và giá trị của hai (hoặc nhiều) mảng và trả về các kết quả phù hợ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 và giá trị 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_uassoc(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+ |
Các ví dụ khác
Thí dụ
So sánh các khóa và giá trị của ba mảng (sử dụng 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"=>"red","b"=>"green","d"=>"blue");
$a3=array("e"=>"yellow","a"=>"red","d"=>"blue");
$result=array_intersect_uassoc($a1,$a2,$a3,"myfunction");
print_r($result);
?>
❮ Tham chiếu mảng PHP