PHP one-dimensional array sorting and multidimensional array sorting _php tutorial

Source: Internet
Author: User
PHP Tutorial One-dimensional array sorting and multidimensional array sorting

See the example of one-dimensional data sorting asort () function and Ksort () function


The Asort () function takes the value of the array in ascending order
$mix = Array ("Clalei" =>10, "Bill" =>50, "Aala" =>100);
Asort ($mix);
Print_r ($mix);
The Ksort () function is based on the keyword ascending of the array
$mix = Array ("Clalei" =>100, "Bill" =>50, "Aala" =>10);
Ksort ($mix);
Print_r ($mix);

?>

One-dimensional data sorting two

The sort () function, which sorts the array in ascending order of letters or numbers (from low to high):

$name = Array ("Clalei", "Bill", "Aala");
Sort ($name);
for ($i =0; $i <3; $i + +) {
echo $name [$i];
}
echo "
";
$price = Array (100,50,10);
Sort ($price);
for ($i =0; $i <3; $i + +) {
echo $price [$i]. "| ";
}

?>

Okay, now let's take a look at the two-dimensional database tutorial sort instance Code

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

In this example, the volume is sorted in descending order, and the edition in ascending order.

Now that you have an array with rows, but Array_multisort () requires an array of columns, so use the following code to get the columns and sort them.
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

Two-dimensional array sorting three

Array_multisort () sorting a two-dimensional array
Array $roughdata, we intend to sort by accurancy.

Array ([0] = = Array ([username] = 10yl [accuracy] = 0.00) [1] = = Array ([username] = 11yl [accuracy] = = 1.00) [2] = = Array ([username] = 12yl [accuracy] = 0.00) [3] = = Array ([username] + 13yl [accu Racy] = 1.00))
Method:
(1) Extracting an array of accuracy columns

foreach ($roughData as $key = = $row) {
$usernames [$key] = $row [' username '];
$accuracy [$key] = $row [' accuracy '];
}

(2) Sorting
Array_multisort ($accuracy, SORT_ASC, $roughData);

When Print_r ($roughData), we will get a two-dimensional array sorted by accuracy ascending order


Let's look at a simple example. Using two-dimensional array sorting method

$array [] = Array ("Age" =>20, "name" = "Li");
$array [] = Array ("Age" =>21, "name" = "AI");
$array [] = Array ("Age" =>20, "name" = "CI");
$array [] = Array ("Age" =>22, "name" = "Di");

foreach ($array as $key = = $value) {
$age [$key] = $value [' Age '];
$name [$key] = $value [' name '];
}

Array_multisort ($age, Sort_numeric,sort_desc, $name, SORT_STRING,SORT_ASC, $array);
Print_r ($array);
?>


http://www.bkjia.com/PHPjc/444956.html www.bkjia.com true http://www.bkjia.com/PHPjc/444956.html techarticle PHP Tutorial One-dimensional array sorting and multidimensional array sorting first look at the example of one-dimensional data ordering asort () function and Ksort () function? The PHP//asort () function takes an array of values in ascending order $mix = Array (Clalei ...

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