PHP One-dimensional two-dimensional array key Sorting Method Example summary, two-dimensional array instance _php tutorial

Source: Internet
Author: User
Tags array example

PHP One-dimensional two-dimensional array key Sorting Method Example summary, two-dimensional array instances


This paper summarizes the key ordering method of PHP one-dimensional two-dimensional array. Share to everyone for your reference. Here's how:

Array sorting in PHP has always been a commonplace problem, let's focus on the implementation of a one-dimensional array and two-dimensional array ordering in PHP, I believe that we have some reference value.

Function: Reorder the array.

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

22 Compare the size of the data element to be sorted, and find that two data elements are exchanged in reverse order until there are no reversed data elements

Imagine that the sorted array R[1..N] is vertically erected, and each data element is treated as a bubble of weight, and the array is scanned from the bottom up, so that the light bubbles that violate the principle are scanned, causing it to "float" upward. So repeatedly. Until the last of any two gases are light on the top, the heavier is down.
Copy the Code code as follows:/**
* Bubble sort (one-dimensional array) (a two-dimensional array of one health sort)
* 22 Compare the size of the data element to be sorted, and find that two data elements are exchanged in reverse order until there are no reversed data elements
* Imagine a sorted array of R[1..N] vertically erected, each data element as a weight of bubbles, from the bottom of the scan array, where the principle of the scanning of light bubbles, so that it "floating upward." So repeated.
* Until the last of any two gases are light in the upper, the heavier is down.
*/
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 Jian exists
if ($array [$j] [$key] < $array [$j -1][$key]) {
$tmp = $array [$j];
$array [$j] = $array [$j-1];
$array [$j-1] = $tmp;
}
}else{//one-dimensional arrays
if ($array [$j] < $array [$j-1]) {
$tmp = $array [$j];
$array [$j] = $array [$j-1];
$array [$j-1] = $tmp;
}
}
}
}
return $array;
}

Array_multisort How to use sorting

Array_multisort () How to use a two-bit array to sort by a specified key value

In this example, a two-dimensional array named $data is defined and then sorted by the following method
Copy the Code code as follows: $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 '];
}

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);
Print_r ($data);

After execution, print the following results:
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
)
)

One-dimensional array ordering we just use sort () to be able, the relative asort ($arr); function, which is the ordering of key values and maintaining the original key-value relationship.

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

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/910587.html www.bkjia.com true http://www.bkjia.com/PHPjc/910587.html techarticle php One-dimensional two-dimensional array key Sorting Method Example summary, two-dimensional array example This paper summarizes the PHP one-dimensional two-dimensional array key ordering method. Share to everyone for your reference. The specific method is as follows: ...

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