Php multi-dimensional array sorting example
-
$ Array = array (
- '0' => array ('3', 'one '),
- '1' => array ('20140901', 'two '),
- '2' => array ('12', 'Three '),
- '3' => array ('13', 'Four '),
- '4' => array ('1', 'five '),
- '5' => array ('3', 'six '),
- );
-
/* Method 1: The uasort () function is used instead of the usort () function. The difference is that uasort () retains the original index and usort () reconstructs the index. */
- Function cmp ($ a, $ B ){
- If ($ a [0] = $ B [0]) {
- Return 0;
- }
- Return ($ a [0] <$ B [0])? -1: 1;
- }
-
Uasort ($ array, "cmp ");
-
Print_r ($ array );
-
Echo"
**********************
";
-
/* Method 2: Use array_multisort */
- Foreach ($ array as $ key => $ value ){
- $ First [$ key] = $ value [0];
- $ Second [$ key] = $ value [1];
- }
- Array_multisort ($ first, SORT_NUMERIC, SORT_ASC, $ second, SORT_STRING, SORT_ASC, $ array );
- Print_r ($ array );
- /*
- Sort order mark:
- SORT_ASC-sort by ascending order
- SORT_DESC-sort by descent
-
Sorting type flag:
- SORT_REGULAR-compare projects by common methods
- SORT_NUMERIC-compare projects by numerical values
- SORT_STRING-compare items by string
- */
>>> For more information, see The php array sorting method < |