Some examples of PHP array sorting functions summarize _php Tutorial

Source: Internet
Author: User
Tags array sort sorts
There are many functions that can be implemented in the array ordering in PHP, and if it is a simple one-dimensional array we can use PHP's own sort function, but we need to write the multidimensional array ourselves.


Back after always worried Ah, so turned over a book to check, to me know the answer. In order to remember them, I decided to come up with a big summary of the array sort function.


The first contact is the PHP array sorting, descending sort problem.

Sort: 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.
Rsort: This function reverses the order of the array (highest to lowest). Delete the original key name and not just reorder it.
Asort: Sorting an array and maintaining an index relationship
Arsort: Reverse-sort an array and maintain an index relationship

Ksort: The array is sorted by key name, preserving the association of the key name to the data
Krsort: The array is reversed by the key name, preserving the association of the key name to the data

Natsort: Sorting alphanumeric strings and preserving the association of the original key/value
Natcasesort: Sort algorithm with Natsort, but not case-sensitive alphabetical sort


PHP array sorting (sort)
Sorting Numeric index arrays:
Functions: Sort (array, [Sort type])
Description: the sort () function sorts the specified array (the first parameter) in ascending order.
The second parameter of the sort function is the specified sort type, which is an optional parameter and the possible values are:
Sort_regular: Default value, do not change type to sort;
Sort_numeric: Sort the values as numbers;
Sort_string: Sorts the values as strings;
An array of 4 and "37″, sorted by number, 4 less than" 37″; sorted by string, 4 greater than "37″;


One or one-D arrays

Suppose there is a one-dimensional array, as follows:


$SORTARR = Array ("name" = "Hiro", "age" = "all", "City" = "Shanghai", "Code" = "200051");
The original array result of the Print_r () output is:


Array ([name] = Hiro [age] = [CITY] = Shanghai [code] = 200051)

1.sort () function: Ascending order according to array subscript;
Print_r () outputs an array with the result (only the array subscript at output, not the key name):

1
Array ([0] = [1] = 200051 [2] = Shanghai [3] = = Hiro)

2.rsort () function: In contrast to the sort () function, the array subscript is sorted in descending order;
Print_r () outputs an array with the result (only the array subscript at output, not the key name):


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

3.asort () function: in ascending order according to the key name of the array;
The result of an array of print_r () output is:

1
Array ([age] = [code] = 200051 [CITY] = Shanghai [name] = Hiro)

4.arsort () function: In contrast to the Asort () function, in descending order according to the key name of the array;
The result of an array of print_r () output is:


Array ([name] = Hiro [City] = Shanghai [code] = 200051 [age] + 23)

5.ksort () function: Ascending order According to the key value of the array;
The result of an array of print_r () output is:


Array ([age] = [CITY] = Shanghai [code] + 200051 [name] = Hiro)

6.krsort () function: In contrast to the Ksort () function, in descending order according to the key value of the array;
The result of an array of print_r () output is:

The code is as follows Copy Code


Array ([name] = Hiro [City] = Shanghai [code] = 200051 [age] + 23)

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

The code is as follows Copy Code
Array ([name] = Hiro [age] = [CITY] = Shanghai [code] = 200051)

8.shuffle () function: Arranges the array order randomly (the order of the permutations is different after each refresh);
The Print_r () output array results in (just one of the random permutations):


Array ([0] = [1] = 200051 [2] = Shanghai [3] = = Hiro)

Two or two-D arrays
Suppose there is a two-dimensional array, as follows:

The code is as follows Copy Code

$person = Array (
Array ("Hiro", "All", "Suzhou"),
Array ("Yoyo", "+", "Shanghai"),
Array ("Janstar", "page", "Xinjiang")
);
The original array result of the Print_r () output is:

1
Array ([0] = = Array ([0] = = Hiro [1] = [2] = Suzhou) [1] = = Array ([0] = yoyo [1] = 25 [2] = = Shanghai) [2] = = Array ([0] = Janstar [1] = [2] = Xinjiang))

The ordering of two-dimensional arrays is based on the key names of each dimension, so you need to write the comparison function extra. Let me give you three examples:

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

The code is as follows Copy Code

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 "is sorted by the first element:";
Print_r ($person);
The results of the output are as follows:

1
Sort by the first element: Array ([0] = = Array ([0] = = Hiro [1] = [2] = Suzhou) [1] = = Array ([0] = Janstar [1 ] [2] = = Xinjiang) [2] = = Array ([0] = yoyo [1] = [2] = Shanghai))

2. In ascending order by the third element of each dimension, the code is as follows:

The code is as follows Copy Code

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 "is sorted by the third element in a positive direction:";
Print_r ($person);
The results of the output are as follows:


Sort by the third element: Array ([0] = = Array ([0] = yoyo [1] = [2] = Shanghai) [1] = = Array ([0] = Hiro [1] = [2] = Suzhou) [2] = = Array ([0] = Janstar [1] = [2] = Xinjiang))

3. In ascending order by the third element of each dimension, the code is as follows:

The code is as follows Copy Code
function Reverse_compare2 ($x, $y) {
if ($x [2] = = $t [2]) {
return 0;
} elseif ($x [2] < $y [2]) {
return 1; Can be reversed when changed
} else {
return-1; Can be reversed when changed
}
}

Usort ($person, Reverse_compare2);

echo "In reverse order of the third element:";
Print_r ($person);
The results of the output are as follows:

1
Reverse-Sort by the third element:

The code is as follows Copy Code
Array ([0] = = Array ([0] = Janstar [1] = [2] = Xinjiang) [1] = = Array ([0] = = Hiro [1] = 2 3 [2] = Suzhou) [2] = = Array ([0] = yoyo [1] = [2] = Shanghai))

Oh, is not the code more points, make a note of the future convenient to consult Ah!

Cases

code as follows copy code

"!--? php
error_reporting (0);

/**
* @ numeric array sorting can be sorted from large to small or from smallest to largest
*/
Function Sortarray ($array, $choice) {
$values = array_values ($array); Set up an array of numeric indexes
$ch = $choice ==0? Min:max;//Parameter $choice is 0 by small to large, otherwise the default is to press from large to small
do {
$val = $ch ($values);//Find out The maximum or minimum value
$key = Array_search ($val, $values);//Gets the key name of the maximum value
$result [$key] = $val;//Put the maximum value in the new array
unset ($values [$key]) ;
} while (count ($values) > 0);
return $result;
}

$array = Array (+, 1, 10000);
$array = Sortarray ($array, 0);
$array = Sortarray ($array, 1);
foreach ($array as $a) {
echo "$a";
}
?

http://www.bkjia.com/PHPjc/631245.html www.bkjia.com true http://www.bkjia.com/PHPjc/631245.html techarticle There are many functions that can be implemented in the array ordering in PHP, and if it is a simple one-dimensional array we can use PHP's own sort function, but we need to write the multidimensional array 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.