Ordering steps for two-dimensional arrays in PHP

Source: Internet
Author: User
Tags sorts
A method of ordering two-dimensional arrays in PHP


/**
* @package???? Bugfree
* @version???? $Id: functionsmain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
*
*
* Sort an two-dimension array by some level the use Array_multisort () function.
*
* Syssortarray ($Array, "Key1", "Sort_asc", "Sort_retular", "Key2" ...)
* @author????????????????????? Chunsheng Wang <Wwccss@263.net>
* @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;
}
?
?
//################# Example #################
$arr = Array (
??? Array (
??????? ' Name '??????? =??? ' Learning ',
??????? ' Size '??????? =??? ' 1235 ',
??????? ' Type '??????? =??? ' Jpe ',
??????? ' Time '??????? =??? ' 1921-11-13 ',
??????? ' Class '??????? =??? ' DD ',
??? ),
??? Array (
??????? ' Name '??????? =??? ' Chinese kung Fu ',
??????? ' Size '??????? =??? ' 153 ',
??????? ' Type '??????? =??? ' Jpe ',
??????? ' Time '??????? =??? ' 2005-11-13 ',
??????? ' Class '??????? =??? ' JJ ',
??? ),
??? Array (
??????? ' Name '??????? =??? ' Programming ',
??????? ' Size '??????? =??? ' + ',
??????? ' Type '??????? =??? ' GIF ',
??????? ' Time '??????? =??? ' 1997-11-13 ',
??????? ' Class '??????? =??? ' DD ',
??? ),
??? Array (
??????? ' Name '??????? =??? ' Chinese kung Fu ',
??????? ' Size '??????? =??? ' A ',
??????? ' Type '??????? =??? ' Jpe ',
??????? ' Time '??????? =??? ' 1925-02-13 ',
??????? ' Class '??????? =??? ' yy ',
??? ),
??? Array (
??????? ' Name '??????? =??? ' Chinese kung Fu ',
??????? ' Size '??????? =??? ' 5 ',
??????? ' Type '??????? =??? ' Icon ',
??????? ' Time '??????? =??? ' 1967-12-13 ',
??????? ' Class '??????? =??? ' RR ',
??? ),
);
?
Print_r ($arr);
?
//Note: Sort by number 153:65 small
$temp = Syssortarray ($arr, "name", "Sort_asc", "type", "Sort_desc", "size", "Sort_asc", "sort_string");
?
Print_r ($temp);
?
?>

?

Other related sorting functions:

?

Sort (Array & $array [, int $sort _flags])

The function sorts the target array, and the elements are arranged in the order of the values from lowest to highest. Note that the function argument is passed by reference, and it is not returning the sorted array. Instead, it simply sorts the current array, regardless of the result, and returns no value.
Note: This function assigns a new key name to the cells in the array. This will delete the original key name and not just reorder it.

$arr = Array (9,5,2,6,8,4,3,1); Sort ($arr); Print_r ($arr);?>

Output: (You can see that the key value association is no longer persisted)

Array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6 [6] = 8 [7] = 9)

Asort (Array & $array [, int $sort _flags])

This function sorts the arrays, the index of the array remains, and the cell is associated. It is used primarily to sort the associative arrays that are important for those cell sequences. It is also passed by reference and does not return a value.

 
  

Output (you can see the key values remain the same):

Array ([5] = 1 [1] = 2 [4] = 4 [0] = 5 [2] = 6 [3] = 8)

Rsort (Array & $array [, int $sort _flags])

This function is similar to the sort function, in which the array is reversed (highest to lowest).

Note:? This function is? array ? Assigns a new key name to the cell in the This will delete the original key name and not just reorder it.

If successful then return? TRUE, the failure is returned? FALSE.

Arsort (Array & $array [, int $sort _flags])

This function reverses the sorting of arrays, the index of the array is maintained and the cell is associated. It is primarily used to sort the associative arrays that are important for the cell order. The Arsort () function is the same as Asort (), except that it sorts the array elements in reverse order (descending).

If successful then return? TRUE, the failure is returned? FALSE.

Ksort (Array & $array [, int $sort _flags])

The difference between ksort and sort is that an array is sorted by key name instead of by value, preserving the association of the key name to the data. This function is mainly used for associative arrays. Returns TRUE if successful, and FALSE if it fails.

Krsort (Array & $array [, int $sort _flags])
See Ksort, this function with Ksort just put ksort after the results of the order.

Natsort (Array & $array)

This function implements a sort algorithm that is similar to how people usually sort alphanumeric strings and maintains the association of the original key/value, which is referred to as the "natural sort". The difference between this algorithm and the usual computer string sorting algorithm (for sort ()) is shown in the example below.

Returns TRUE if successful, and FALSE if it fails.
There's a very classic example in the PHP manual.

 
  

The example above will output: standard sorting

Array ([0] = img1.png [1] = img10.png [2] = = Img12.png [3] = = img2.png)

Natural Order Sorting

Array ([3] = img1.png [2] = img2.png [1] = = img10.png [0] = = img12.png)

There are also some array sorting functions u that start with (user) and are custom sort functions. Time reason that's not much to write about.

  • 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.