A simple PHP multi-dimensional array sorting. Sorting by an element in a multi-dimensional array is also very easy to implement in PHP. a function can call a callback function, you can also sort php built-in functions such as array_multisor by an element in a multi-dimensional array. this is also very easy to implement in PHP. a function can call a callback function, you can also use functions provided by php.
For example, array_multisort ($ a, $ B), $ a, and $ B are two arrays. after sorting, the 3rd elements in the $ a array are ranked first, then, the third element of $ B is ranked first regardless of its size in $ B. Check the program running result below:
| The code is as follows: |
|
|
$ A = array (100,80, 50,10, 0 );
$ B = array ("c", "f", "q", "e", "z ");
Array_multisort ($ a, $ B );
Var_dump ($ );
Var_dump ($ B );
?> |
Running result:
| The code is as follows: |
|
|
Array (5) {[0] => int (0) [1] => int (10) [2] => int (50) [3] = & gt; int (80) [4] = & gt; int (100 )}
Array (5) {[0] => string (1) "z" [1] => string (1) "e" [2] => string (1) "q" [3] => string (1) "f" [4] => string (1) "c "} |
Sort by custom functions
| The code is as follows: |
|
|
$ Arr = array (
'Index' => array ('name' => 'homepage ', 'order' => 3 ),
'Intro' => array ('name' => 'Enterprise profile ', 'order' => 2 ),
'News' => array ('name' => 'news list', 'order' => 1 ),
'Product' => array ('name' => 'product Center', 'order' => 4 ),
'Message' => array ('name' => 'guest message', 'order' => 7 ),
'Position' => array ('name' => 'talent recruitment ', 'order' => 6 ),
'Contact '=> array ('name' => 'Contact us', 'order' => 5)
);
Uasort ($ arr, 'cmp ');
Public function cmp ($ a, $ B ){
Return $ a ['order']-$ B ['order'];
} |
$ Arr is sorted by order size ......
Metrics such as array_multisor...