search
Homephp教程php手册高手详谈PHP二维数组

高手详谈PHP二维数组

Jun 13, 2016 am 11:04 AM
phpInternet accesslearnaboutdevelopMulti-useritarraymaster

PHP经过长时间的发展,很多用户都很了解它了,上网收集了一些关于PHP二维数组的相关知识,这里和大家一起分享一下,PHP本身是有一个多维数组排序的函数的。

<ol class="dp-xml"><li class="alt"><span><span>boolarray_multisort(array$ar1[,mixed$arg[,mixed$...[,array$...]]]) </span></span></li></ol>

下面是手册当中对于array_multisort函数的描述:

array_multisort()可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。关联(string)键名保持不变,但数字键名会被重新索引。输入数组被当成一个表的列并以行来排序――这类似于SQL的ORDERBY子句的功能。第一个数组是要排序的主要数组。数组中的行(值)比较为相同的话就按照下一个输入数组中相应值的大小来排序,依此类推。从手册中看到,PHP本身的多维排序,是按照第一个数组进行排序,并且调整后面的顺序。像这样的数组:
<ol class="dp-xml">
<li class="alt"><span><span>array('id'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>array(1,3,2),  </span></span></li>
<li class="">
<span>'data'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>array('a','c','b')) </span>
</li>
</ol>

只要按照id进行多维排序,就可以了。但是很多时候,我们构造出的PHP二维数组是这样的:

<ol class="dp-xml">
<li class="alt"><span><span>array(  </span></span></li>
<li class="">
<span>array('id'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>1,'data'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'a'),  </span>
</li>
<li class="alt">
<span>array('id'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>3,'data'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'c'),  </span>
</li>
<li class="">
<span>array('id'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>2,'data'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'b')  </span>
</li>
<li class="alt"><span>); </span></li>
</ol>

数组的元素是按行排列的,需要按其中的一列进行排序。PHP好像也没提供类似矩阵转置的函数,所以不能直接使用array_multisort进行多维排序。不过只需要先把排序的列抽取出来,作为第一个参数传给array_multisort即可。

<ol class="dp-xml">
<li class="alt"><span><span>functionmulti_array_sort($multi_array,$sort_key,$</span><span class="attribute"><font color="#ff0000">sort</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">SORT_ASC</font></span><span>){  </span></span></li>
<li class=""><span>if(is_array($multi_array)){  </span></li>
<li class="alt"><span>foreach($multi_arrayas$row_array){  </span></li>
<li class=""><span>if(is_array($row_array)){  </span></li>
<li class="alt"><span>$key_array[]=$row_array[$sort_key];  </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt"><span>return-1;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt"><span>return-1;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>array_multisort($key_array,$sort,$multi_array);  </span></li>
<li class=""><span>return$multi_array;  </span></li>
<li class="alt"><span>}  </span></li>
</ol>

以上就是简单的PHP二维数组的介绍,希望对大家有帮助。


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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool