Summary of some php array sorting function examples _ PHP Tutorial

Source: Internet
Author: User
Tags function examples
Summary of some php array sorting function examples. In php, there are many functions for array sorting. if it is a simple one-dimensional array sorting, we can use the sorting function provided by php, but we need to write the multi-dimensional array by ourselves. In php, there are many functions for array sorting. if it is a simple one-dimensional array sorting, we can use the sorting function provided by php, but we need to write the multi-dimensional array by ourselves.


After I came back, I was always worried. so I read the book and gave me the answer. In order to remember them, I decided to give a big summary of an array sorting function.


The first issue is PHP array sorting and descending sorting.

Sort: This function assigns a new key name to the cell in array. This will delete the original key name, not just the reorder.
Rsort: This function sorts the array in reverse order (up to the lowest ). Deleting the original key name is not just a re-sorting.
Asort: sorts arrays and maintains the index relationship.
Arsort: sorts arrays in reverse order and maintains the index relationship.

Ksort: sorts the array by key name and retains the association between key names and data.
Krsort: sorts the array in reverse order by key name, and retains the association between key names and data.

Natsort: sorts alphanumeric strings and keeps the original key/value Association.
Natcasesort: same as natsort, but not case sensitive.


PHP array sorting (sort)
Sort numeric index arrays:
Function: sort (array, [sort type])
Note: the sort () function sorts the specified array (the first parameter) in ascending order.
The second parameter of the sort function is used to specify the sorting type. it is an optional parameter and the possible value is:
SORT_REGULAR: default value. sorting is performed without changing the type;
SORT_NUMERIC: sorts values as numbers;
SORT_STRING: sorts values as strings;
If the array contains 4 and "37", sort by number, 4 is less than "37"; sort by string, 4 is greater than "37 ″;


1. one-dimensional array

Suppose there is a one-dimensional array, as shown below:


$ SortArr = array ("name" => "hiro", "age" => "23", "city" => "Shanghai ", "code" = & gt; "200051 ");
The original array output by print_r () is:


Array ([name] => hiro [age] => 23 [city] => Shanghai [code] => 200051)

1. sort () function: sort in ascending order according to the array subscript;
The output result of the print_r () array is (only the array subscript is output, not the key name ):

1
Array ([0] = & gt; 23 [1] = & gt; 200051 [2] = & gt; Shanghai [3] = & gt; hiro)

2. rsort () function: opposite to sort () function, sort in descending order according to the array subscript;
The output result of the print_r () array is (only the array subscript is output, not the key name ):


Array ([0] => hiro [1] => Shanghai [2] => 200051 [3] => 23)

3. asort () function: sort the keys in the array in ascending order;
The output array result of print_r () is:

1
Array ([age] => 23 [code] => 200051 [city] => Shanghai [name] => hiro)

4. arsort () function: opposite to asort () function, sort the values in descending order based on the key names of the array;
The output array result of print_r () is:


Array ([name] => hiro [city] => Shanghai [code] => 200051 [age] => 23)

5. ksort () function: sort the key values of the array in ascending order;
The output array result of print_r () is:


Array ([age] => 23 [city] => Shanghai [code] => 200051 [name] => hiro)

6. krsort () function: opposite to ksort () function, the krsort () function is sorted in descending order based on the key values of the array;
The output array result of print_r () is:

The code is as follows:


Array ([name] => hiro [city] => Shanghai [code] => 200051 [age] => 23)

7. reverse_array () function: returns the reverse order of the current array;
The output array result of print_r () is:

The code is as follows:
Array ([name] => hiro [age] => 23 [city] => Shanghai [code] => 200051)

8. shuffle () function: sorts arrays randomly (the order after each refresh is different );
The output array result of print_r () is (just a random arrangement ):


Array ([0] = & gt; 23 [1] = & gt; 200051 [2] = & gt; Shanghai [3] = & gt; hiro)

2. two-dimensional array
Assume there is a two-dimensional array, as shown below:

The code is as follows:

$ Person = array (
Array ("hiro", "23", "suzhou "),
Array ("yoyo", "25", "shanghai "),
Array ("janstar", "28", "xinjiang ")
);
The original array output by print_r () is:

1
Array ([0] => Array ([0] => hiro [1] => 23 [2] => suzhou) [1] => Array ([0] => yoyo [1] => 25 [2] => shanghai) [2] => Array ([0] => janstar [1] => 28 [2] => xinjiang ))

The sorting of two-dimensional arrays is based on the key names of each dimension. Therefore, you need to write additional comparison functions. Here are three examples:

1. sort the first element of each dimension in ascending order. the code is as follows:

The code is as follows:

Function compare0 ($ x, $ y ){
If ($ x [0] === t [0]) {
Return 0;
} Elseif ($ x [0] <$ y [0]) {
Return-1;
} Else {
Return 1;
}
}

Usort ($ person, compare0 );
Echo "positive sorting by the first element :";
Print_r ($ person );
The output result is as follows:

1
Forward sorting by the first element: Array ([0] => Array ([0] => hiro [1] => 23 [2] => suzhou) [1] => Array ([0] => janstar [1] => 28 [2] => xinjiang) [2] => Array ([0] => yoyo [1] => 25 [2] => shanghai ))

2. sort the third element of each dimension in ascending order. the code is as follows:

The code is as follows:

Function compare2 ($ x, $ y ){
If ($ x [2] === t [2]) {
Return 0;
} Elseif ($ x [2] <$ y [2]) {
Return-1;
} Else {
Return 1;
}
}

Usort ($ person, compare2 );
Echo "positive sorting by the third element :";
Print_r ($ person );
The output result is as follows:


Forward sorting by the third element: Array ([0] => Array ([0] => yoyo [1] => 25 [2] => shanghai) [1] => Array ([0] => hiro [1] => 23 [2] => suzhou) [2] => Array ([0] => janstar [1] => 28 [2] => xinjiang ))

3. sort the third element of each dimension in ascending order. the code is as follows:

The code is as follows:
Function reverse_compare2 ($ x, $ y ){
If ($ x [2] === t [2]) {
Return 0;
} Elseif ($ x [2] <$ y [2]) {
Return 1; // reverse after changing
} Else {
Return-1; // reverse after changing
}
}

Usort ($ person, reverse_compare2 );

Echo "reverse sorting by the third element :";
Print_r ($ person );
The output result is as follows:

1
Sort by the third element in reverse order:

The code is as follows:
Array ([0] => Array ([0] => janstar [1] => 28 [2] => xinjiang) [1] => Array ([0] => hiro [1] => 23 [2] => suzhou) [2] => Array ([0] => yoyo [1] => 25 [2] => shanghai ))

Oh, isn't there more code? you can easily check it after you write it down!

Example

The code is as follows:

Error_reporting (0 );

/**
* @ The number array can be sorted in ascending order or ascending order.
*/
Function sortArray ($ array, $ choice ){
$ Values = array_values ($ array); // create an array of numeric indexes.
$ Ch = $ choice = 0? Min: max; // The $ choice parameter is 0 in ascending order.
Do {
$ Val = $ ch ($ values); // you can specify the maximum or minimum value.
$ Key = array_search ($ val, $ values); // obtain the maximum key name.
$ Result [$ key] = $ val; // save the maximum value to the new array.
Unset ($ values [$ key]);
} While (count ($ values)> 0 );
Return $ result;
}

$ Array = array (100,100 0, 1, 10000 );
// $ Array = sortArray ($ array, 0 );
$ Array = sortArray ($ array, 1 );
Foreach ($ array as $ ){
Echo "$ ";
}
?>

Sort comes with the sorting function, but we need to write the multi-dimensional array by ourselves ....

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.