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 use php functions such as array_multisort ($,
Sorting by an element in a multi-dimensional array is also very easy to implement in PHP. a function can call a callback function, or a built-in function in 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 will rank first no matter the size of $ 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 );
- ?>
The running result 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 "}
The code for sorting 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 ......