Example explanation of PHP function array_multisort() to implement multi-dimensional array sorting_PHP tutorial

WBOY
Release: 2016-07-15 13:29:56
Original
834 people have browsed it

For The Listing J example gives us a concrete explanation of how the PHP function array_multisort() works:

<ol class="dp-xml">
<li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>data</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>(array("id" =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span> 1, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "Boney M", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3),  </span>
</li>
<li class="">
<span>array("id" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 2, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "Take That", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1),  </span>
</li>
<li class="alt">
<span>array("id" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "The Killers", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 4),  </span>
</li>
<li class="">
<span>array("id" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 4, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "Lusain", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3),  </span>
</li>
<li class="alt">
<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> $value) {  </span>
</li>
<li class=""><span>$name[$key] = $value['name'];  </span></li>
<li class="alt"><span>$rating[$key] = $value['rating'];  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>array_multisort($rating, $name, $data); print_r($data);</span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Copy after login

Here, we simulate a row and column in the $data array array. I then used the PHP function array_multisort() to reorder the data set, first by rating, and then, if the ratings were equal, by name. Its output is as follows:

Array ([0] => Array

(

[id] => 2

[name] => Take That

[rating] = > 1

) [1] => Array

(

[id] => 1

[name] => Boney M

[rating] => 3

)

[2] => Array

(

[id] => 4

[name] => Lusain

[rating] => )

[3] => Array

(

[id] => 3

[name] => The Killers

[rating] => 4

)

)

The PHP function array_multisort() is one of the most useful functions in PHP, and it has a very wide range of applications. In addition, as you can see in the example, it can sort multiple unrelated arrays, it can also use one element as the basis for the next sort, and it can also sort database result sets.


http://www.bkjia.com/PHPjc/446344.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446344.htmlTechArticleFor Listing J example, we specifically explain how the PHP function array_multisort() works: ? php $ data = array (array("id"= 1,"name"= "BoneyM","rating"= 3), array("id"= 2,"name"=...
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