Experts talk about PHP two-dimensional arrays in detail_PHP tutorial

WBOY
Release: 2016-07-15 13:27:13
Original
959 people have browsed it

After a long period of development of PHP, many users are familiar with it. I have collected some relevant knowledge about PHP two-dimensional arrays on the Internet. I will share it with you here. PHP itself has a multi-dimensional array sorting Functional.

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

The following is the description of the array_multisort function in the manual:

array_multisort() can be used to sort multiple arrays at one time , or sort a multidimensional array according to one or more dimensions. Association (string) key names remain unchanged, but numeric key names are re-indexed. The input array is treated as a table column and sorted by row - this is similar to the functionality of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array are compared to be the same, they are sorted according to the size of the corresponding value in the next input array, and so on. From the manual, we can see that PHP's own multi-dimensional sorting is to sort the first array and adjust the subsequent order. An array like this:
<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>
Copy after login

Just do multidimensional sorting by id and you're good to go. But many times, the PHP two-dimensional array we construct looks like this:

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

The elements of the array are arranged in rows and need to be sorted by one of the columns. PHP does not seem to provide a function similar to matrix transposition, so array_multisort cannot be used directly for multidimensional sorting. But you only need to extract the sorted column first and pass it to array_multisort as the first parameter.

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

The above is a simple introduction to two-dimensional arrays in PHP. I hope it will be helpful to everyone.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446530.htmlTechArticleAfter a long period of development of PHP, many users are familiar with it. I have collected some information about PHP two-dimensional arrays online. Related knowledge, here to share with you, PHP itself has more...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template