To sort by an element in a multidimensional array is also very easy to implement in PHP, a function call a callback function is done, it can also be PHP's own function
such as Array_multisort ($a, $b), $a, $b is two arrays, if after sorting, the 3rd element of the $a array is ranked first, then $b's third element regardless of his size in the $b will be ranked first. Look at the results of the program running below:
The code is as follows |
Copy Code |
$a =array (100,80,50,10,0); $b = Array ("C", "F", "Q", "E", "Z"); Array_multisort ($a, $b); Var_dump ($a); Var_dump ($b); ?> |
Operation Result:
The code is as follows |
Copy Code |
Array (5) {[0]=> int (0) [1]=> Int (ten) [2]=> int ([3]=> int) [4]=> int (100)} Array (5) {[0]=> string (1) "Z" [1]=> string (1) "E" [2]=> string (1) "Q" [3]=> string (1) "F" [4]=> string (1) "C"} |
Custom Function Implementation Ordering
The code is as follows |
Copy Code |
$arr = Array (
' Index ' =>array (' name ' = ' = ' home ', ' order ' =>3),
' Intro ' =>array (' name ' = ' Company profile ', ' Order ' =>2),
' News ' =>array (' name ' = ' Press ', ' Order ' =>1),
' Product ' =>array (' name ' = ' Products center ', ' Order ' =>4),
' Message ' =>array (' name ' = ' guest message ', ' Order ' =>7),
' Position ' =>array (' name ' = ' Jobs ', ' order ' =>6),
' Contact ' =>array (' name ' = = ' Contacting us ', ' order ' = 5)
);
Uasort ($arr, ' CMP ');
Public Function cmp ($a, $b) {
return $a [' order ']-$b [' Order '];
} |
At this time $arr is to order the size of order, hehe ...
http://www.bkjia.com/PHPjc/633103.html www.bkjia.com true http://www.bkjia.com/PHPjc/633103.html techarticle to sort by an element in a multidimensional array, in PHP is also very easy to implement, a function call a callback function is done, you can also PHP's own functions such as Array_multisor ...