3 Ways to sort PHP two-dimensional arrays and custom functions share _php instances

Source: Internet
Author: User
About sorting generally we are in the database or NoSQL (Eg:redis) First order and then output to the program directly to use, but sometimes we need to sort by PHP directly to the array, and in PHP to store data to use the most is the object and arrays, but the more processing is the array, Because there is a very rich library of built-in libraries (in fact objects can be understood to be an array in some way), these libraries can largely help us implement certain functions. The commonly used system functions are sort, asort, Arsort, Ksort, Krsort, and so on, here I mainly say to two-dimensional array of the sort, both ways:

First, with PHP self-array_multisort function sort
Copy the Code code as follows:

$data = Array ();
$data [] = Array (' volume ' = +, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 1);
$data [] = Array (' volume ' = =, ' edition ' = 6);
$data [] = Array (' volume ' = = 98, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 6);
$data [] = Array (' volume ' = +, ' edition ' = 7);

Get a list of columns
foreach ($data as $key = $row)
{
$volume [$key] = $row [' volume '];
$edition [$key] = $row [' Edition '];
}

Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);

Print_r ($data);
?>

Output Result:

Copy the Code code as follows: Array
(
[0] = = Array
(
[Volume] = 98
[Edition] = 2
)
[1] = = Array
(
[Volume] = 86
[Edition] = 1
)
[2] = = Array
(
[Volume] = 86
[Edition] = 6
)
[3] = = Array
(
[Volume] = 85
[Edition] = 6
)
[4] = = Array
(
[Volume] = 67
[Edition] = 2
)
[5] = = Array
(
[Volume] = 67
[Edition] = 7
)
)

The official documentation for Array_multisort also has more detailed instructions: http://www.php.net/manual/zh/function.array-multisort.php

II. Ordering of custom functions 1
Copy the Code code as follows:
$data = Array ();
$data [] = Array (' volume ' = +, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 1);
$data [] = Array (' volume ' = =, ' edition ' = 6);
$data [] = Array (' volume ' = = 98, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 6);
$data [] = Array (' volume ' = +, ' edition ' = 7);

Get a list of columns
foreach ($data as $key = $row)
{
$volume [$key] = $row [' volume '];
$edition [$key] = $row [' Edition '];
}

$ret = ArraySort ($data, ' volume ', ' desc ');

Print_r ($ret);

/**
* @desc arraysort php Two-dimensional array sorting by the specified key arrays
* @param array $arr will be sorted
* @param string $keys Specify the sorted key
* @param string $type sort type ASC | Desc
* @return Array
*/
function ArraySort ($arr, $keys, $type = ' asc ') {
$keysvalue = $new _array = Array ();
foreach ($arr as $k = = $v) {
$keysvalue [$k] = $v [$keys];
}
$type = = ' ASC '? Asort ($keysvalue): Arsort ($keysvalue);
Reset ($keysvalue);
foreach ($keysvalue as $k = = $v) {
$new _array[$k] = $arr [$k];
}
return $new _array;
}
?>
Output Result:
Copy the Code code as follows: Array
(
[3] = = Array
(
[Volume] = 98
[Edition] = 2
)

[4] = = Array
(
[Volume] = 86
[Edition] = 6
)

[1] = = Array
(
[Volume] = 86
[Edition] = 1
)

[2] = = Array
(
[Volume] = 85
[Edition] = 6
)

[5] = = Array
(
[Volume] = 67
[Edition] = 7
)

[0] = = Array
(
[Volume] = 67
[Edition] = 2
)

)

One of the differences between this custom function and the system function is that the custom function only supports sorting on a key, and it needs to be executed multiple times to support the ordering of multiple keys; While the system function Array_multisort can be one-time to multiple keys and can specify more than one collation, the system function is quite powerful, recommend the use of system functions, after all, is the C bottom-level implementation, here is just an example of how to sort the array by a custom function, Of course, this custom function can also continue to expand to support more collations. In the rankings, leaderboards, scores and other scenes used in a lot of.

III. Ordering of custom functions 2

The following function is to sort a given two-dimensional array according to the specified key value, and first look at the function definition:
Copy CodeThe code is as follows:
function Array_sort ($arr, $keys, $type = ' asc ') {
$keysvalue = $new _array = Array ();
foreach ($arr as $k = = $v) {
$keysvalue [$k] = $v [$keys];
}
if ($type = = ' asc ') {
Asort ($keysvalue);
}else{
Arsort ($keysvalue);
}
Reset ($keysvalue);
foreach ($keysvalue as $k = = $v) {
$new _array[$k] = $arr [$k];
}
return $new _array;
}
It can sort the two-dimensional arrays by the specified key values, or you can specify ascending or descending sorting (the default is ascending), and the usage example:
Copy CodeThe code is as follows:
$array = Array (
Array (' name ' = ' phone ', ' brand ' = ' Nokia ', ' Price ' =>1050),
Array (' name ' = ' laptop ', ' brand ' = ' Lenovo ', ' Price ' =>4300),
Array (' name ' = ' razor ', ' brand ' = ' Philips ', ' Price ' =>3100),
Array (' name ' = = ' treadmill ', ' brand ' = ' three and ' pine stone ', ' price ' =>4900),
Array (' name ' = ' watch ', ' brand ' = ' casio ', ' Price ' =>960),
Array (' name ' = ' + ' LCD tv ', ' Brand ' = ' sony ', ' Price ' =>6299),
Array (' name ' = ' laser printer ', ' brand ' = ' hp ', ' Price ' =>1200)
);

$ShoppingList = Array_sort ($array, ' price ');
Print_r ($ShoppingList);

Above is the $array of this two-dimensional array according to ' Price ' from low to high sort.

  • Related Article

    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.