PHP array_intersect_uassoc ()ฟังก์ชั่น
ตัวอย่าง
เปรียบเทียบคีย์และค่าของสองอาร์เรย์ และคืนค่าที่ตรงกัน (โดยใช้ฟังก์ชันการเปรียบเทียบคีย์ที่ผู้ใช้กำหนด):
<?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);
?>
ความหมายและการใช้งาน
ฟังก์ชัน array_intersect_uassoc() จะเปรียบเทียบคีย์และค่า ของอาร์เรย์สองชุด (หรือมากกว่า) แล้วส่งกลับค่าที่ตรงกัน
หมายเหตุ:ฟังก์ชันนี้ใช้ฟังก์ชันที่ผู้ใช้กำหนดเพื่อเปรียบเทียบคีย์!
ฟังก์ชันนี้เปรียบเทียบคีย์และค่าของอาร์เรย์ตั้งแต่สองตัวขึ้นไป และส่งคืนอาร์เรย์ที่มีรายการจากarray1ที่มีอยู่ใน array2 , array3เป็นต้น
ไวยากรณ์
array_intersect_uassoc(array1, array2, array3, ..., myfunction)
ค่าพารามิเตอร์
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 |
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับอาร์เรย์ที่มีรายการจากarray1ที่มีอยู่ในอาร์เรย์อื่นๆ ทั้งหมด |
---|---|
รุ่น PHP: | 5+ |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
เปรียบเทียบคีย์และค่าของอาร์เรย์สามชุด (ใช้ฟังก์ชันที่ผู้ใช้กำหนดเองเพื่อเปรียบเทียบคีย์) และคืนค่าที่ตรงกัน:
<?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);
?>
❮ การอ้างอิงอาร์เรย์ PHP