Home >Backend Development >PHP Problem >How to sort query result set in php
php method to sort the query result set: 1. Forward sorting, the code is [case 'asc': asort($refer)]; 2. Reverse sorting, the code is [case 'desc': arsort($refer)]; 3. Natural sorting, the code is [case 'nat':].
【Related learning recommendations: php graphic tutorial】
php method to sort the query result set:
The complete code is as follows:
/** * list_sort_by()对查询结果集进行排序 * @param array $list 查询结果 * @param string $field 排序的字段名 * @param array $sortby 排序类型 * asc正向排序 desc逆向排序 nat自然排序 * @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': // 正向排序 asort($refer); break; case 'desc':// 逆向排序 arsort($refer); break; case 'nat': // 自然排序 natcasesort($refer); break; } foreach ( $refer as $key=> $val) $resultSet[] = &$list[$key]; return $resultSet; } return false; }
For more related learning, please pay attentionphptraining column!
The above is the detailed content of How to sort query result set in php. For more information, please follow other related articles on the PHP Chinese website!