-
-
$array = array( - '0' => array('3', 'one'),
- '1' => array('101' , 'two'),
- '2' => array('12', 'three'),
- '3' => array('13', 'four'),
- '4' => array ('1', 'five'),
- '5' => array('3', 'six'),
- );
/*Method 1: Using uasort () function instead of usort() function. The difference is that uasort() will maintain the original index and usort() will rebuild 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 flag:
- SORT_ASC – as per Sort in ascending order
- SORT_DESC - Sort in descending order
Sort type flags:
- SORT_REGULAR - Compare items in the usual way
- SORT_NUMERIC - Compare items in numerical order
- SORT_STRING - Compare items in strings
- */
-
Copy code
>>> For more information, please view the complete list of php array sorting methods
|