Sometimes, in order to achieve a certain purpose, it is necessary to sort two-dimensional arrays. Now let’s share the implementation method.
$arr=array (
'1' => array ( 'date' => '2011-08-18', 'num' => 5 ) ,
'2' => array ( 'date' => '2011-08-20', 'num' => 3 ) ,
'3' => array ( 'date' => '2011-08-17', 'num' => 10 )
) ; $result = sysSortArray($arr,'num'); The effect after running this is:
$arr=array (
'1' => array ( 'date' => '2011-08-18', 'num' => 3 ) ,
'2' => array ( 'date' => '2011-08-20', 'num' => 5 ) ,
'3' => array ( 'date' => '2011-08-17', 'num' => 10 )
) ;The functions used:
/**
* Sort an two-dimension array by some level two items use array_multisort() function.
*
* sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2";……)
* @author lamp100
* @param array $ArrayData the array to sort.
* @param string $KeyName1 the first item to sort by.
* @param string $SortOrder1 the order to sort by("SORT_ASC"|"SORT_DESC")
* @param string $SortType1 the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
* @return array sorted array.
*/
function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
{
if(!is_array($ArrayData))
{
return $ArrayData;
}
// Get args number.
$ArgCount = func_num_args();
// Get keys to sort by and put them to SortRule array.
for($I = 1;$I < $ArgCount;$I ++)
{
$Arg = func_get_arg($I);
if(!eregi("SORT",$Arg))
{
$KeyNameList[] = $Arg;
$SortRule[] = '$'.$Arg;
}
else
{
$SortRule[] = $Arg;
}
}
// Get the values according to the keys and put them to array.
foreach($ArrayData AS $Key => $Info)
{
foreach($KeyNameList AS $KeyName)
{
${$KeyName}[$Key] = $Info[$KeyName];
}
}
// Create the eval string and eval it.
$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
eval ($EvalString);
return $ArrayData;
}
In addition: the array_multisort function is also very powerful. For details, you can refer to the PHP manual, which is explained in detail.
We can use the array_multisort() function. The array_multisort() function sorts multiple arrays or multidimensional arrays.
The arrays in the parameters are treated as columns of a table and sorted by rows - this is similar to the functionality of the ORDER BY clause of SQL. The first array is the main array to be sorted. If the rows (values) in the array are compared to the same, they will be sorted according to the size of the corresponding value in the next input array, and so on.
The first parameter is an array, and each subsequent parameter may be an array, or one of the following sort order flags (the sort flag is used to change the default sort order):
•SORT_ASC - Default, sorted in ascending order. (A-Z)
•SORT_DESC - Order in descending order. (Z-A)
You can then specify the sort type:
•SORT_REGULAR - Default. Arrange each item in a regular order.
•SORT_NUMERIC - Order each item in numerical order.
•SORT_STRING - Order each item alphabetically.
Syntax: array_multisort(array1, sorting order, sorting type, array2, array3...)
•array1: Required. Specifies the input array.
•sorting order: optional. Specify the order of arrangement. Possible values are SORT_ASC and SORT_DESC.
•sorting type: optional. Specify the sorting type. Possible values are SORT_REGULAR, SORT_NUMERIC, and SORT_STRING.
•array2: Optional. Specifies the input array.
•array3: Optional. Specifies the input array.
The string key name will be preserved, but the numeric key will be reindexed, starting at 0 and incrementing by 1. You can set the sort order and sort type after each array. If not set, each array parameter uses the default value.
Here is an example:
<?php
$arr = '';
echo 'The two-dimensional array is as follows: '.'<br / >';
for($i=0; $i<=5; $i++)
{
$arr[$i]['val'] = mt_rand(1, 100);
$arr[$i]['num'] = mt_rand(1, 100);
}
echo '<pre>';
print_r($arr);
echo '</pre>';
echo 'Extract the key from the two-dimensional array to val and form another array alone: '.'<br / >';
foreach ($arr as $key => $row)
{
$vals[$key] = $row['val'];
$nums[$key] = $row['num'];
}
echo '<pre>';
print_r($vals);
echo '</pre>';
echo 'Sort it: '.'<br / >';
array_multisort($vals, SORT_ASC, $arr);
echo '<pre>';
print_r($vals);
echo '</pre>';
?>
Running results:
The two-dimensional array is as follows:
Array
(
[0] => Array
(
[val] => 46
[num] => 49
)
[1] => Array
(
[val] => 8
[num] => 24
)
[2] => Array
(
[val] => 37
[num] => 3
)
[3] => Array
(
[val] => 32
[num] => 35
)
[4] => Array
(
[val] => 19
[num] => 38
)
[5] => Array
(
[val] => 30
[num] => 37
)
)
Extract the key from the two-dimensional array to val and form another array alone:
Array
(
[0] => 46
[1] => 8
[2] => 37
[3] => 32
[4] => 19
[5] => 30
)
Sort them:
Array
(
[0] => 8
[1] => 19
[2] => 30
[3] => 32
[4] => 37
[5] => 46
)
We will get a 2D array sorted in ascending order of val.