Home > Backend Development > PHP Tutorial > Introduction to specific methods of sorting two-dimensional arrays in PHP_PHP tutorial

Introduction to specific methods of sorting two-dimensional arrays in PHP_PHP tutorial

WBOY
Release: 2016-07-15 13:29:19
Original
744 people have browsed it

What we are introducing to you today is about The following is the description of the array_multisort function in the manual:

array_multisort() can be used to sort multiple PHP two-dimensional arrays at one time Sort to sort, or to sort a multidimensional array according to one or more dimensions.

Associative (string) key names remain unchanged, but numeric key names will be 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.

As seen from the manual, PHP two-dimensional array 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 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 PHP two-dimensional array sorting are arranged by rows, and they 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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446383.htmlTechArticleWhat we will introduce to you today is the following description of the array_multisort function in the manual: array_multisort() can be used Sort multiple PHP two-dimensional arrays at once,...
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