توابع uksort() در PHP
مثال
استفاده از تابع مقایسه کاربر تعریف شده برای مرتب کردن عناصر آرایه $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"); ?>
Definition and Usage
The uksort() function sorts the array by key name using a user-defined comparison function.
Tip:Please use uasort() The function sorts the array by key value using a user-defined comparison function, which uses a user-defined comparison function for sorting.
Syntax
uksort(array,myfunction);
Parameter | Description |
---|---|
array | Required. Specifies the array to be sorted. |
myfunction | Optional. Defines a string that specifies the comparison function that can be called. If the first parameter is less than, equal to, or greater than the second parameter, the comparison function must return an integer less than, equal to, or greater than 0. |
Description
The uksort() function uses a user-defined comparison function to sort the array by key name and maintain the index relationship.
Returns TRUE if successful, otherwise FALSE.
If the array to be sorted needs to be sorted with an unusual standard, then this function should be used.
Custom functions should accept two parameters, which will be filled with a pair of key names in the array. The comparison function must return an integer less than, equal to, or greater than zero when the first parameter is less than, equal to, or greater than the second parameter.
Technical Details
Return Value: | Returns TRUE if successful, FALSE otherwise. |
PHP Version: | 4+ |