An example of a PHP one-dimensional two-dimensional array key sorting method _php Tips

Source: Internet
Author: User
Tags php programming

This article summarizes the PHP one-dimensional two-dimensional array key sorting method. Share to everyone for your reference. The specific methods are as follows:

Array ordering in PHP has always been a cliché question, let's focus on the implementation of one-dimensional array and two-dimensional array sequencing in PHP, I believe we have a certain reference value.

Features: reordering of arrays.

Description: Bubble sort (one-dimensional array) (two-dimensional array, some sort of health)

22 Compare the size of the data element to be sorted, and find that the order of the two data elements is reversed until there is no inverse data element

Imagine that the sorted array R[1..N] is vertically erected, consider each data element to be a bubble with weight, scan the array from bottom to top, and "float" the light bubbles that violate the principle. This is done over and over again. Until the last of the two, the light is on, and the heavy is up.

Copy Code code as follows:
/**
* Bubble sort (one-dimensional array) (two-dimensional array of some health sort)
* 22 Compare the size of the data element to be sorted, and find that the order of the two data elements is reversed until there is no inverse data element
* Imagine that the sorted array R[1..N] is vertically erected, that each data element is considered to be a weight bubble, that the array is scanned from the bottom up, and that the light bubbles that violate the principle are scanned to "float" upward. So repeatedly.
* Until the last of the two, the light is on the top, the heavy in the next.
*/
function Bubble_sort ($array, $key =null) {
$count = count ($array);
if ($count < 0) {
return false;
}
for ($i = 0; $i < $count; $i + +) {
for ($j = $count-1; $j > $i; $j-) {
if ($key && isset ($array [$key]) {//two-D array health
if ($array [$j] [$key] < $array [$j -1][$key]) {
$tmp = $array [$j];
$array [$j] = $array [$j-1];
$array [$j-1] = $tmp;
}
}else{//one-dimensional array
if ($array [$j] < $array [$j-1]) {
$tmp = $array [$j];
$array [$j] = $array [$j-1];
$array [$j-1] = $tmp;
}
}
}
}
return $array;
}

Array_multisort sorting using method

Array_multisort () How to sort two-bit arrays by specified key values

In this example, a two-dimensional array named $data is defined and then sorted by the following method

Copy Code code as follows:
$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 '];
}

Arranges data in descending order according to volume, sorted by edition ascending order
Sort the $data as the last parameter in a common key
Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);
Print_r ($data);

After execution, the results are printed as follows:

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
)
)

One-dimensional array ordering we can just use sort () and correspond to the Asort ($arr); function, which is to sort key values and maintain the original key-value relationship.

Same principle, rsort (); Arsort (); Krsort (); Functions in addition to sorting are sorted in descending order, others with sort (); Rsort (); Ksort (); Same.

I hope this article will help you with your PHP programming.

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.