How do php two-dimensional arrays be sorted by a field? _php Tutorials

Source: Internet
Author: User
There are a lot of ready-made functions for sorting data, but if you want to sort the specified fields there is no built-in function, below I have sorted out some related array sorting functions to communicate with everyone to learn.

Let's not talk about anything else. Two-dimensional arrays sort instances by a field

The code is as follows Copy Code
/**
* @author Yebihai http://www.bKjia.c0m
* @desc Ascending and descending by a field in a two-dimensional array
* @data
* $testData = Array (
Array (' price ' =>19),
Array (' price ' =>121),
Array (' price ' =>115),
Array (' price ' =>113),
Array (' Price ' =>112)
);
*/
Class sortclass{
Ascending
function Sortarrayasc ($preData, $sortType = ' price ') {
$sortData = Array ();
foreach ($preData as $key _i = $value _i) {
$price _i = $value _i[$sortType];
$min _key = ";
$sort _total = count ($sortData);
foreach ($sortData as $key _j = $value _j) {
if ($price _i< $value _j[$sortType]) {
$min _key = $key _j+1;
Break
}
}
if (Empty ($min _key)) {
Array_push ($sortData, $value _i);
}else {
$sortData 1 = array_slice ($sortData, 0, $min _key-1);
Array_push ($sortData 1, $value _i);
if (($min _key-1) < $sort _total) {
$sortData 2 = Array_slice ($sortData, $min _key-1);
foreach ($sortData 2 as $value) {
Array_push ($sortData 1, $value);
}
}
$sortData = $sortData 1;
}
}
return $sortData;
}
Descending
function Sortarraydesc ($preData, $sortType = ' price ') {
$sortData = Array ();
foreach ($preData as $key _i = $value _i) {
$price _i = $value _i[$sortType];
$min _key = ";
$sort _total = count ($sortData);
foreach ($sortData as $key _j = $value _j) {
if ($price _i> $value _j[$sortType]) {
$min _key = $key _j+1;
Break
}
}
if (Empty ($min _key)) {
Array_push ($sortData, $value _i);
}else {
$sortData 1 = array_slice ($sortData, 0, $min _key-1);
Array_push ($sortData 1, $value _i);
if (($min _key-1) < $sort _total) {
$sortData 2 = Array_slice ($sortData, $min _key-1);
foreach ($sortData 2 as $value) {
Array_push ($sortData 1, $value);
}
}
$sortData = $sortData 1;
}
}
return $sortData;
}
}

To sort out some of the functions of array sorting

The data sort function has

The sort () function is used to sort the array cells from low to high.
The Rsort () function is used to sort the array cells from high to low.
The Asort () function is used to sort the array cells from low to high and keep the index relationship.
The Arsort () function is used to sort the array cells from high to low and keep the index relationship.
The Ksort () function is used to sort the array cells from low to high by their key names.
The Krsort () function is used to sort the array cells by their key names from high to low.
The Array_multisort () function sorts multiple arrays or multidimensional arrays

http://www.bkjia.com/PHPjc/632714.html www.bkjia.com true http://www.bkjia.com/PHPjc/632714.html techarticle There are a lot of ready-made functions for sorting data, but if you want to sort the specified fields without the built-in functions, I'll sort out some of the related array sorting functions to communicate with you .

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