How to sort query result set in php

coldplay.xixi
Release: 2023-03-05 09:54:01
Original
2680 people have browsed it

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':].

How to sort query result set in php

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; }
Copy after login

For more related learning, please pay attentionphptrainingcolumn!

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!