Home  >  Article  >  Backend Development  >  How to sort query result set in php

How to sort query result set in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-27 09:46:072770browse

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;
}

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!

Statement:
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