Share a PHP on a field in a two-dimensional array ascending and descending sort class

Source: Internet
Author: User
Share a PHP class for ascending and descending a field of a two-dimensional array
  * @desc to sort a field of a two-dimensional array * @data * $testData = Array (' Newprice ' =>9), array (' Newprice ' =>21), Array (' Newprice ' =>15), array (' Newprice ' =>13), array (' Newprice ' =>12));        */class sortclass{//Ascending function Sortarrayasc ($preData, $sortType = ' Newprice ') {$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 = ' Newprice ') {$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; }}?>
?
  • 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.