PHP Multidimensional array sequencing implementation Code _php tutorial

Source: Internet
Author: User
In PHP, the sorting of arrays including one-dimensional array sorting and multi-dimensional array ordering, let me elaborate on the use of PHP array sorting functions and custom functions to achieve data sorting method.

Singular group sort

Sort function Ascending order

BOOL Sort (array & $array [, int $sort _flags= sort_regular])

The code is as follows Copy Code

$fruits = Array ("Lemon", "orange", "banana", "apple");
Sort ($fruits);
Var_dump ($fruits);
?>

-rsort descending order--

The code is as follows Copy Code

$fruits = Array ("Lemon", "orange", "banana", "apple");
Rsort ($fruits); Var_dump ($fruits);
?>

For example, there is a multidimensional array:

The code is as follows Copy Code
$arr = Array (
' d ' = = Array (' id ' = = 5, ' name ' = + 1, ' age ' = 7),
' b ' = = Array (' id ' = 2, ' name ' = + 3, ' age ' = 4),
' a ' = = Array (' id ' = 8, ' name ' = ' = ', ' age ' = 5),
' c ' = = Array (' id ' = = 1, ' name ' = + 2, ' age ' = 2)
);

You need to sort the age items in a two-dimensional array.


PHP's built-in function array_multisort () is required,


Array_multisort () requires an array containing columns, so use the following code to get the columns, and then sort them.

The code is as follows Copy Code
Get a list of columns
foreach ($data as $key = = $row) {
$volume [$key] = $row [' volume '];
$edition [$key] = $row [' Edition '];
}

Sort data in descending order according to volume, in ascending order of edition
The $data as the last parameter, sorted by the Universal key
Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);

The data set is now sorted and the results are as follows:
Volume | Edition
-------+--------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7


Custom functions:

The code is as follows Copy Code
function Multi_array_sort ($multi _array, $sort _key, $sort =sort_asc)
{
if (Is_array ($multi _array))
{
foreach ($multi _array as $row _array)
{
if (Is_array ($row _array))
{
$key _array[] = $row _array[$sort _key];
}
Else
{
return false;
}
}
}
Else
{
return false;
}
Array_multisort ($key _array, $sort, $multi _array);
return $multi _array;
}


Processing

TD bgcolor= "#FFE7CE" height= "width=" 464 "> code as follows
copy code
print_r (Multi_array_sort ($arr, ' age '));
//Output
Array
(
[c] = + Array
(
[id] + 1
[name] + 2
[age] + 2
)

[b] = = Array
(
[id] = 2
[name] + 3
[age] + 4
)

[a] + = array
(
[id] =&G T 8
[Name] =
[age] = 5
)

[d] = = Array
(
[id] + 5
[name] + 1
[age] = 7
)

)

Actually there are many other ways to use it in the sort order, such as PHP's array of Arsort (), Asort (), Ksort (), Krsort (), Natsort (), Natcasesort (), Rsort (), Usort ( ), Array_multisort () and Uksort ().


http://www.bkjia.com/PHPjc/445296.html www.bkjia.com true http://www.bkjia.com/PHPjc/445296.html techarticle in PHP, the sorting of arrays including one-dimensional array sorting and multi-dimensional array ordering, let me elaborate on the use of PHP array sorting functions and custom functions to achieve data sorting method. Singular ...

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