Home php教程 php手册 谈谈关于PHP数组合并技巧

谈谈关于PHP数组合并技巧

Jun 13, 2016 am 11:04 AM
php use about merge exist us Skill array article of meet question

我们在使用PHP数组的时候可能遇到PHP数组合并的问题,文章这里介绍了PHP数组合并、PHP数组的排序。PHP数组排序实际上就是将PHP数组进行排序。在1995年 中,PHP2.0发布了。第二版定名为PHP/FI(Form Interpreter)。PHP/FI加入了对mSQL的支持,从此建立了PHP在动态网页开发上的地位。

到了1996年底,有15000个网站使用 PHP/FI;时间到了1997年中,使用PHP/FI的网站数字超过五万个。而在1997年中,开始了第三版的开发计划,开发小组加入了 Zeev Suraski 及 Andi Gutmans,而第三版就定名为PHP3。2000年,PHP4.0又问世了,其中增加了许多新的特性。在本文中也就是对数据库查询出来的结果集进行排序。数据库查询结果有时候不能直接使用,比如mysql等用in语句出来的结果,因此需要对结果进行某种方式的排序。这时候就需要进行PHP数组排序了。对数据库结果进行排序请看下面的示例:本例中data 数组中的每个单元表示一个表中的一行。这是典型的数据库存放数组数据的方式。例子中的数据如下:
<ol class="dp-xml">
<li class="alt"><span><span>volume | edition  </span></span></li>
<li class=""><span>-------+--------  </span></li>
<li class="alt"><span>67 |       2  </span></li>
<li class=""><span>86 |       1  </span></li>
<li class="alt"><span>85 |       6  </span></li>
<li class=""><span>98 |       2  </span></li>
<li class="alt"><span>86 |       6  </span></li>
<li class=""><span>67 |       7 </span></li>
</ol>
Copy after login

数据全都存放在名为 data 的数组中。这通常是通过循环从数据库取得的结果,例如 mysql_fetch_assoc()(实际上这个函数你可以和mysql_fetch_assoc()函数看成一样,具体的差别你可以看PHP的手册里面关于key的差别)。

<ol class="dp-xml">
<li class="alt"><span><span>$data[] = array('volume' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 67, 'edition' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 2);  </span></span></li>
<li class="">
<span>$data[] = array('volume' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 86, 'edition' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1);  </span>
</li>
<li class="alt">
<span>$data[] = array('volume' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 85, 'edition' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 6);  </span>
</li>
<li class="">
<span>$data[] = array('volume' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 98, 'edition' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 2);  </span>
</li>
<li class="alt">
<span>$data[] = array('volume' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 86, 'edition' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 6);  </span>
</li>
<li class="">
<span>$data[] = array('volume' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 67, 'edition' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 7); </span>
</li>
</ol>
Copy after login

本例中将把 volume 降序排列,把 edition 升序排列。

现在有了包含有行的数组,但是 array_multisort() 需要一个包含列的数组,因此用以下代码来取得列,然后排序。

<ol class="dp-xml">
<li class="alt"><span><span>// 取得列的列表  </span></span></li>
<li class="">
<span>foreach ($data as $</span><span class="attribute"><font color="#ff0000">key</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $row) {  </span>
</li>
<li class="alt"><span>$volume[$key]  = $row['volume'];  </span></li>
<li class=""><span>$edition[$key] = $row['edition'];  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>// 将数据根据 volume 降序排列,根据 edition 升序排列  </span></li>
<li class=""><span>// 把 $data 作为最后一个参数,以通用键排序  </span></li>
<li class="alt"><span>array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);  </span></li>
</ol>
Copy after login

PHP数组合并在排好序了,结果如下:

<ol class="dp-xml">
<li class="alt"><span><span>volume | edition  </span></span></li>
<li class=""><span>-------+--------  </span></li>
<li class="alt"><span>98 |       2  </span></li>
<li class=""><span>86 |       1  </span></li>
<li class="alt"><span>86 |       6  </span></li>
<li class=""><span>85 |       6  </span></li>
<li class="alt"><span>67 |       2  </span></li>
<li class=""><span>67 |       7 </span></li>
</ol>
Copy after login

实际上真正在排序的时候还有很多方法可以使用,比如php的array数组自带的 arsort(),asort(),ksort(),krsort(),natsort(),natcasesort(),rsort(),usort(),array_multisort() 和 uksort()。


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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles