PHP two-dimensional array sorting detailed _php example

Source: Internet
Author: User

Sometimes in order to achieve a certain goal, you need to sort the two-dimensional array, and now share its implementation methods.

Copy Code code 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 '); the effect after this operation 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 to use:


/**


* Sort an Two-dimension array by some level two the items 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 ' the ' the ' the '.


* @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: Array_multisort function is also very powerful, detailed can refer to the PHP manual, which said in detail.

We can use the Array_multisort () function. The Array_multisort () function sorts multiple arrays or multidimensional arrays.
The array in the argument is treated as a column of a table and sorted by row-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 compare to the same, they are sorted according to the size of the corresponding values in the next input array, and so on.
The first argument 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 arrangement order):
SORT_ASC-Default, in ascending order. (A-Z)
Sort_desc-in descending order. (Z-A)
You can then specify the sort type:
sort_regular-Default. Arranges each item in a regular order.
Sort_numeric-Arranges each item in numerical order.
sort_string-Arranges each entry in alphabetical order.
Syntax: Array_multisort (array1,sorting order,sorting type,array2,array3 ...)
array1: Required. A specified array of inputs.
sorting order: Optional. Specify the order of arrangement. The possible values are SORT_ASC and Sort_desc.
sorting type: Optional. Specify the sort type. The possible values are Sort_regular, Sort_numeric, and sort_string.
array2: Optional. A specified array of inputs.
array3: Optional. A specified array of inputs.
The string key name is preserved, but the number key is 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 Code code as follows:

<?php

$arr = ';

The echo ' 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 extracts the key from the two-dimensional array to Val and separate it into another array: '. ' <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> ';

?>



Run Result:


Two-dimensional arrays are as follows:


Array


(


[0] =&gt; Array


(


[Val] =&gt; 46


[Num] =&gt; 49


)


[1] =&gt; Array


(


[Val] =&gt; 8


[Num] =&gt; 24


)


[2] =&gt; Array


(


[Val] =&gt; 37


[Num] =&gt; 3


)


[3] =&gt; Array


(


[Val] =&gt; 32


[Num] =&gt; 35


)


[4] =&gt; Array


(


[Val] =&gt; 19


[Num] =&gt; 38


)


[5] =&gt; Array


(


[Val] =&gt; 30


[Num] =&gt; 37


)


)


Extracts a key from a two-dimensional array to Val and separate it into another array:


Array


(


[0] =&gt; 46


[1] =&gt; 8


[2] =&gt; 37


[3] =&gt; 32


[4] =&gt; 19


[5] =&gt; 30


)


To sort it:


Array


(


[0] =&gt; 8


[1] =&gt; 19


[2] =&gt; 30


[3] =&gt; 32


[4] =&gt; 37


[5] =&gt; 46


)


We'll get a two-dimensional array sorted by Val in ascending order.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.