Sometimes in order to achieve a certain purpose, we need to sort the two-dimensional array, and now share the method of its implementation.
Copy CodeThe code is as follows:
$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 '), so the effect after running 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 function used:
/**
* Sort an Two-dimension array by some level the use Array_multisort () function.
*
* Syssortarray ($Array, "Key1", "Sort_asc", "Sort_retular", "Key2", ...)
* @author lamp100
* @param array $ArrayData the array to sort.
* @param string $KeyName 1 The first item to sort by.
* @param string $SortOrder 1 The order to sort by ("Sort_asc" | " Sort_desc ")
* @param string $SortType 1 the Sort type ("Sort_regular" | " Sort_numeric "|" Sort_string ")
* @return Array sorted array.
*/
function Syssortarray ($ArrayData, $KeyName 1, $SortOrder 1 = "Sort_asc", $SortType 1 = "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, in detail you can see 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 column of a table and sorted by rows-this is similar to the function of the SQL ORDER by clause. The first array is the primary array to sort. If the rows (values) in the array are the same, they are sorted according to the size of the corresponding values in the next input array, and so on.
The first parameter is an array, and each subsequent argument may be an array, or it may be one of the following sort order flags (the sort flags are used to change the default order):
SORT_ASC-By default, in ascending order. (A-Z)
Sort_desc-sorted in descending order. (Z-A)
You can then specify the sort type:
sort_regular-Default. Arranges each item in a regular order.
sort_numeric-Sorts each item in numerical order.
sort_string-Arranges 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. The possible values are SORT_ASC and Sort_desc.
sorting type: Optional. Specifies the sort type. The 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 is preserved, but the number key is re-indexed, starting at 0 and incrementing by 1. You can set the sort order and sort type after each array. If not set, the default value is used for each array parameter.
Here is an example:
Copy CodeThe code is as follows:
$arr = ";
Echo ' 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 '
';
Print_r ($arr);
Echo '
';
Echo ' Extracts the key from the two-dimensional array to Val, which is separated into another array: '. '
';
foreach ($arr as $key = $row)
{
$vals [$key] = $row [' Val '];
$nums [$key] = $row [' num '];
}
Echo '
';
Print_r ($vals);
Echo '
';
Echo ' Sorts it: '. '
';
Array_multisort ($vals, SORT_ASC, $arr);
Echo '
';
Print_r ($vals);
Echo '
';
?>
Operation Result:
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 separate it into another array:
Array
(
[0] = 46
[1] = 8
[2] = 37
[3] = 32
[4] = 19
[5] = 30
)
To sort it:
Array
(
[0] = 8
[1] = 19
[2] = 30
[3] = 32
[4] = 37
[5] = 46
)
We will get a two-dimensional array sorted by Val ascending order.
http://www.bkjia.com/PHPjc/824986.html www.bkjia.com true http://www.bkjia.com/PHPjc/824986.html techarticle sometimes in order to achieve a certain purpose, we need to sort the two-dimensional array, and now share the method of its implementation. Copy the code code as follows: $arr =array (' 1 ' = array (' Date ' = ' 2011-08 ...