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 .