Maison > développement back-end > tutoriel php > PHP函数array_multisort()实现多维数组排序的示例讲解_PHP教程

PHP函数array_multisort()实现多维数组排序的示例讲解_PHP教程

WBOY
Libérer: 2016-07-15 13:29:56
original
844 Les gens l'ont consulté

对于Listing J示例为我们具体说明了PHP函数array_multisort()的工作方式:

<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>
<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">></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></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>
Copier après la connexion

这里,我们在$data数组中模拟了一个行和列数组。然后,我使用PHP函数array_multisort()对数据集合进行重排,首先是根据rating进行排序,然后,如果rating相等的话,再根据name排序。它的输出结果如下:

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

)

[3] => Array

(

[id] => 3

[name] => The Killers

[rating] => 4

)

)

PHP函数array_multisort()是PHP中最有用的函数之一,它有非常广泛的应用范围。另外,就如你在例子中所看到的,它能对多个不相关的数组进行排序,也可以使用其中的一个元素作为下次排序的基础,还可以对数据库结果集进行排序。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446344.htmlTechArticle对于 Listing J示例为我们具体说明了PHP函数array_multisort()的工作方式: ? php $ data = array (array("id"= 1,"name"= "BoneyM","rating"= 3), array("id"= 2,"name"=...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal