This article mainly introduces the PHP implementation of a two-dimensional array of a key ordering method, involving PHP for array traversal, comparison, sorting and other related operations skills, the need for friends can refer to the next
Specific as follows:
/** * Sort Query Result set * @access public * @param array $list Query result * @param string $field sort Field Name * @param string $sortby sort type (as C Forward Sort desc Reverse sort Nat natural Sort) * @return Array */function list_sort_by ($list, $field, $sortby = ' asc ') {if (Is_array ($list ) {$refer = $resultSet = Array (); foreach ($list as $i = + $data) {$refer [$i] = & $data [$field]; } switch ($sortby) {case ' ASC '://forward sort asort ($refer); Break Case ' desc '://Reverse sort arsort ($refer); Break Case ' NAT '://Natural sort natcasesort ($refer); Break } foreach ($refer as $key = + $val) {$resultSet [] = & $list [$key]; } return $resultSet; } return false; }/** * Example *: Based on the ID key values of the two-dimensional array in descending order (that is, the higher the ID of the row in the previous)? */$list = Array (0 = = Array (' id ' = = 1, ' name ' = ' = ' first '), 1 = = array (' id ' = = 3, ' name ' => ; ' Third '), 2 = = Array (' id ' = = 2, ' name ' = ' second ') ), 3 = = Array (' id ' = = 4, ' name ' = = ' IV '),);//Solution $new_list = list_sort_by ($list, ' id ', ' desc ');p Rint_r ($ New_list);
The results of the operation are as follows:
Array ([ 0] = = Array ( [id] = 4 [name] + fourth ) [1] = = Array ( [id] = 3< C8/>[name] and third ) [2] = = array ([id] = 2 [name] +-second ) [3] = = Array ( [id] = 1 [name] + first ))
Summary: The above is the entire content of this article, I hope to be able to help you learn.