PHP uksort()ฟังก์ชั่น
ตัวอย่าง
จัดเรียงองค์ประกอบของอาร์เรย์ $arr ตามคีย์โดยใช้ฟังก์ชันการเปรียบเทียบที่ผู้ใช้กำหนด:
<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$arr=array("a"=>4,"b"=>2,"c"=>8,"d"=>6);
uksort($arr,"my_sort");
?>
ความหมายและการใช้งาน
ฟังก์ชัน uksort() จัดเรียงอาร์เรย์ตามคีย์โดยใช้ฟังก์ชันเปรียบเทียบที่ผู้ใช้กำหนด
เคล็ดลับ:ใช้ฟังก์ชัน uasort() เพื่อจัดเรียงอาร์เรย์ตามค่าโดยใช้ฟังก์ชันการเปรียบเทียบที่ผู้ใช้กำหนด
ไวยากรณ์
uksort(array, myfunction)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
array | Required. Specifies the array to sort |
myfunction | Optional. 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 |
รายละเอียดทางเทคนิค
คืนมูลค่า: | จริงกับความสำเร็จ FALSE เมื่อล้มเหลว |
---|---|
รุ่น PHP: | 4+ |
❮ การอ้างอิงอาร์เรย์ PHP