Use built-in functions to solve PHP multi-dimensional array sorting problems_PHP tutorial

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

When learning PHP, you may encounter the problem of sorting multi-dimensional arrays in PHP. Here we will introduce the solution to the problem of sorting multi-dimensional arrays in PHP. I will share it with you here. I hope it will be helpful to everyone.

Compare the size and sort according to the keys of the array. Needless to say, PHP has many convenient built-in functions, krsort(), ksort() If you compare the size based on the value of the array, it will be more troublesome. If there are no duplicate values, you can first use array_flip() to exchange keys and values; then krsort(), and then exchange them back in array_flip() to compare the sizes. If there are duplicate values, some sorting algorithms must be used. However, PHP has a very convenient callback function uasort() that will maintain the original index and usort() that will rebuild the index. PHP multidimensional array sorting code:
<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li>
<li class=""><span>functioncmp($a,$b){  </span></li>
<li class="alt"><span>if($a[”nums”]==$b[”nums”]){  </span></li>
<li class=""><span>return0;  </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span>return($a[”nums”]</span><span class="tag"><</SPAN><SPAN>$b[”nums”])?-1:1;  </SPAN></SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>arr</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>Array</SPAN><SPAN>(  </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=attribute>0</SPAN><SPAN>=</SPAN><SPAN class=tag>></span><span>Array(  </span>
</li>
<li class="">
<span>“username”=</span><span class="tag">></span><span>owen,  </span>
</li>
<li class="alt">
<span>“nums”=</span><span class="tag">></span><span>2,  </span>
</li>
<li class=""><span>),  </span></li>
<li class="alt">
<span></span><span class="attribute">1</span><span>=</span><span class="tag">></span><span>Array(  </span>
</li>
<li class="">
<span>“username”=</span><span class="tag">></span><span>d5s,  </span>
</li>
<li class="alt">
<span>“nums”=</span><span class="tag">></span><span>5,  </span>
</li>
<li class=""><span>),  </span></li>
<li class="alt">
<span></span><span class="attribute">2</span><span>=</span><span class="tag">></span><span>Array(  </span>
</li>
<li class="">
<span>“username”=</span><span class="tag">></span><span>pt,  </span>
</li>
<li class="alt">
<span>“nums”=</span><span class="tag">></span><span>3,  </span>
</li>
<li class=""><span>),  </span></li>
<li class="alt"><span>);  </span></li>
<li class=""><span>uasort($arr,“cmp”);  </span></li>
<li class="alt">
<span>echo‘</span><span class="tag"><</SPAN><SPAN class=attribute>prestyle</SPAN><SPAN class=tag-name>prestyle</SPAN><SPAN>=”text-align:left”</SPAN><SPAN class=tag>></span><span>’;  </span>
</li>
<li class=""><span>print_r($arr);  </span></li>
<li class="alt">
<span>echo‘</span><span class="tag"><span class="tag-name">pre</span><span class="tag">></span><span>’;  </span></span>
</li>
<li class="">
<span></span><span class="tag">?></span><span> </span>
</li>
<li class="alt"><span> </span></li>
<li class=""><span>Array  </span></li>
<li class="alt"><span>(  </span></li>
<li class="">
<span>[0]=</span><span class="tag">></span><span>Array  </span>
</li>
<li class="alt"><span>(  </span></li>
<li class="">
<span>[username]=</span><span class="tag">></span><span>owen  </span>
</li>
<li class="alt">
<span>[nums]=</span><span class="tag">></span><span>2  </span>
</li>
<li class=""><span>)  </span></li>
<li class="alt"><span> </span></li>
<li class="">
<span>[2]=</span><span class="tag">></span><span>Array  </span>
</li>
<li class="alt"><span>(  </span></li>
<li class="">
<span>[username]=</span><span class="tag">></span><span>pt  </span>
</li>
<li class="alt">
<span>[nums]=</span><span class="tag">></span><span>3  </span>
</li>
<li class=""><span>)  </span></li>
<li class="alt"><span> </span></li>
<li class="">
<span>[1]=</span><span class="tag">></span><span>Array  </span>
</li>
<li class="alt"><span>(  </span></li>
<li class="">
<span>[username]=</span><span class="tag">></span><span>d5s  </span>
</li>
<li class="alt">
<span>[nums]=</span><span class="tag">></span><span>5  </span>
</li>
<li class=""><span>)  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>) </span></li>
</ol>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446522.htmlTechArticleWhen learning PHP, you may encounter PHP multi-dimensional array sorting problems. Here we will introduce PHP multi-dimensional array sorting problems. I will share the solution with you here, I hope it will be helpful to everyone...
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