search
Homephp教程php手册如何利用php array_multisort函数 对数据库结果进行复杂排序

如何利用php array_multisort函数 对数据库结果进行复杂排序

Jun 13, 2016 am 11:49 AM
arrayphpone timefunctionusecomplexhowrightsortdatadatabaseresultconductneedfirst

首先讲一下需求:数据库中有4个字段分别是id,volume,edition,name. 要求对查询结果按照volume+edition从大到小排序。
下面将一下array_multisort函数
array_multisort() 可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。

关联(string)键名保持不变,但数字键名会被重新索引。

排序顺序标志:
SORT_ASC – 按照上升顺序排序
SORT_DESC – 按照下降顺序排序

排序类型标志:
SORT_REGULAR – 将项目按照通常方法比较
SORT_NUMERIC – 将项目按照数值比较
SORT_STRING – 将项目按照字符串比较

每个数组之后不能指定两个同类的排序标志。每个数组后指定的排序标志仅对该数组有效 – 在此之前为默认值 SORT_ASC 和 SORT_REGULAR。

输入数组被当成一个表的列并以行来排序——这类似于 SQL 的 ORDER BY 子句的功能。第一个数组是要排序的主要数组。数组中的行(值)比较为相同的话就按照下一个输入数组中相应值的大小来排序,依此类推。

本函数的参数结构有些不同寻常,但是非常灵活。第一个参数必须是一个数组。接下来的每个参数可以是数组或者是下面列出的排序标志。

这样我们现在有这样一组数据

复制代码 代码如下:


// 这是一组从数据库查询出来的结果
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
//我们需要先做出一个volume+edition的数组来
foreach($data as $val){
     $arr[] = $val['volume'] + $val['edition']; 
}
// 将$arr根据降序排列
// 把 $data 作为最后一个参数,以通用键排序
array_multisort($arr, SORT_DESC, $data);


这样就实现了我们需要的功能
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.