Sometimes, in order to achieve a certain purpose, it is necessary to sort a two-dimensional array. Now I will share how to achieve it.
$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 like 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 )
) ; Function 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) $EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
eval ($EvalString);
return $ArrayData;
}
In addition: the array_multisort function is also very powerful. For details, please refer to the PHP manual, which is very detailed.
We can use the array_multisort() function. The array_multisort() function sorts multiple arrays or multidimensional arrays.
The array in the parameter is treated as a table column and sorted by row - this is similar to the functionality of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array compare to be 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 sorting flags are used to change the default sort order):
•SORT_ASC - Default, by Sort in ascending order. (A-Z)
•SORT_DESC - Sort in descending order. (Z-A)
You can then specify the type of sort:
•SORT_REGULAR - Default. Arrange each item in regular order.
•SORT_NUMERIC - Sort each item in numerical order.
•SORT_STRING - Sort each item in alphabetical order.
Syntax: array_multisort(array1,sorting order,sorting type,array2,array3...)
•array1: required. Specifies the input array.
•Sort order: Optional. Specify the order of sorting. Possible values are SORT_ASC and SORT_DESC.
•sorting type: optional. Specifies 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.
String key names will be preserved, but numeric keys will be re-indexed, starting at 0 and increasing by 1. The sort order and sort type can be set after each array. If not set, each array parameter will use its default value.
The following is an example:
Copy the code The code is as follows:
$arr = '';
echo 'The two-dimensional array is as follows:'.'
';
for($i=0; $i<=5; $i++)
{
$arr[$i]['val'] = mt_rand(1, 100);
$arr[$i]['num'] = mt_rand(1, 100);
}
echo '
';<br> print_r($arr);<br> echo '
';
echo 'Extract from a two-dimensional array The key is val, separated into another array: '.'
';
foreach ($arr as $key => $row)
{
$vals[$key] = $row['val'];
$nums[$key] = $row['num'];
}
echo '
';<br> print_r( $vals);<br> echo '
';
echo 'Sort it:'.'
';
array_multisort($vals, SORT_ASC, $arr);
echo '
';<br> print_r($vals);<br> echo '
';
?>
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
( > => 19
37
)
)
Extract the key val from the two-dimensional array and form it into another array:
Array
(
[0] => 46
[1] => 8
[2] => 37
[3] => 32
[4] => 19
[5] => 30
)
to it Sort:
Array
(
[0] => 8
[1] => 19
[2] => 30
[3] => 32
[4] => 37
[5] => 46
)
We will get a two-dimensional array sorted by val in ascending order.
http://www.bkjia.com/PHPjc/824986.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/824986.htmlTechArticle
Sometimes in order to achieve a certain purpose, it is necessary to sort a two-dimensional array. Now I will share the method of achieving it. Copy the code The code is as follows: $arr=array ( '1' = array ( 'date' = '2011-08...