PHP two-dimensional array ordering 3 methods and custom function sharing _php instance

Source: Internet
Author: User
Tags array sort function definition

In general, we are sorted by database or NoSQL (Eg:redis) and then exported to the program for direct use. But there are times when we need to sort the arrays directly from PHP, while in PHP we use most of the objects and arrays to store the data. Because there are very rich built-in function libraries (in fact, the object to some extent can also be understood as an array), these libraries can largely help us achieve some functionality. Commonly used system functions are sort, asort, Arsort, Ksort, Krsort, and so on, here I would like to say a two-dimensional array of the sort, two methods:

First, the use of PHP with array_multisort function sorting

Copy Code code as follows:

<?php

$data = Array ();
$data [] = Array (' volume ' =>, ' Edition ' => 2);
$data [] = Array (' volume ' =>, ' edition ' => 1);
$data [] = Array (' volume ' =>, ' Edition ' => 6);
$data [] = Array (' volume ' =>, ' 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 results:

Copy 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 document on Array_multisort also has a more detailed description: http://www.php.net/manual/zh/function.array-multisort.php

Two, custom function sort 1

Copy Code code as follows:

<?php
$data = Array ();
$data [] = Array (' volume ' =>, ' Edition ' => 2);
$data [] = Array (' volume ' =>, ' edition ' => 1);
$data [] = Array (' volume ' =>, ' Edition ' => 6);
$data [] = Array (' volume ' =>, ' 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 sort According to the specified key array
* @param array $arr arrays to 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 results:
Copy 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 for a key, and it needs to be executed multiple times to support the ordering of multiple key keys; The system function Array_multisort can be one-time to multiple key and can specify multiple collation, system function is still quite powerful, recommend the use of system functions, after all, is the C-level implementation, here is just an example of a custom function to order the array, Of course, this custom function can also continue to expand to support more collations. In the rankings, rankings, scores and other scenes used in a lot of.

Three, custom function sort 2

The following function is to sort a given two-dimensional array by the specified key value, first look at the function definition:

Copy Code code 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 a two-dimensional array by the specified key value, or you can specify ascending or descending sort (default is ascending), using the example:
Copy Code code as follows:

$array = Array (
Array (' name ' => ' cell 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);


The above is a sort of $array this two-dimensional array in terms of ' price ' from low to high.

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.