PHP usort()ฟังก์ชั่น
ตัวอย่าง
จัดเรียงองค์ประกอบของอาร์เรย์ $a โดยใช้ฟังก์ชันการเปรียบเทียบที่ผู้ใช้กำหนด:
<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$a=array(4,2,8,6);
usort($a,"my_sort");
?>
ความหมายและการใช้งาน
ฟังก์ชัน usort() จัดเรียงอาร์เรย์โดยใช้ฟังก์ชันเปรียบเทียบที่ผู้ใช้กำหนด
ไวยากรณ์
usort(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