This article illustrates the custom array sort functions and sorting classes implemented by PHP. Share to everyone for your reference, specific as follows:
*
* * Two-dimensional array custom sort function
* Uasort ($arr, function_name)
*
**/
$arr = array (
array (' A ' =>1, ' B ' => ' C '),
array (' A ' =>4, ' B ' => ' a '),
array (' A ' =>5, ' B ' => ' G '),
array (' A ' =>7, ' B ' => ' F '),
Array (' A ' =>6, ' B ' => ' e ')
);
function Compare_arr ($x, $y) {
if ($x [' B ']< $y [' B ']) {
return-1;
} else if ($x [' B ']> $y [' B ']) {return
1;
} else{return
0;
}
Uasort ($arr, ' Compare_arr ');
foreach ($arr as $a) {
echo $a [' a ']. ' => '. $a [' B ']. ' <br/> ';
}
Custom sort classes in the manual:
class Multisort {var $key; The key in your array//Sort function argument is an array to arrange the index sort type functions run ($myarray, $key _to_sort, $type _of_sort = ") {$
This->key = $key _to_sort;
if ($type _of_sort = = ' desc ') uasort ($myarray, Array ($this, ' myreverse_compare '));
else Uasort ($myarray, Array ($this, ' mycompare '));
return $myarray;
}//Positive sequence function Mycompare ($x, $y) {if ($x [$this->key] = = $y [$this->key]) return 0;
else if ($x [$this->key] < $y [$this->key]) return-1;
else return 1;
//Reverse function Myreverse_compare ($x, $y) {if ($x [$this->key] = = $y [$this->key]) return 0;
else if ($x [$this->key] > $y [$this->key]) return-1;
else return 1; }
}
More interested in PHP related content readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP string (String) Usage summary", "PHP for XML file Operating skills summary", " PHP error and exception handling method summary, "PHP operation and operator Usage Summary", "PHP basic Grammar Introductory Course", "PHP object-oriented Programming Introduction Course", "Php+mysql Database Operation Introduction Course" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.