Heim  >  Artikel  >  Backend-Entwicklung  >  php 二维数组排序的两个例子

php 二维数组排序的两个例子

WBOY
WBOYOriginal
2016-07-25 08:56:34856Durchsuche
本文介绍下,php中二维数组排序的二个例子,都比较简单,正在学习php数组排序的朋友,可以作个参考,也是不错的。

分享一段代码,实现php中二维数组的排序。 代码:

<?php
/**
* 二维数组排序
* by bbs.it-home.org
*/
 $good = array();
 
 for($i = 0; $i<7 ; $i++ ){
     
     $good[$i]['price']  = rand(1,10000);
    $good[$i]['hot']    = rand(1,100);
     $good[$i]['follow'] = rand(1,1000);    
 
 }
 
 echo '<pre class="brush:php;toolbar:false">';
 
print_r($good);

echo '
'; $hot=array(); $follow=array(); foreach($good as $k=>$v){ $hot[$k] = $v['hot']; $follow[$k] = $v['follow']; } //二维数组排序 //方法一,先按hot字段降序 ,再按follow降序 array_multisort($hot,SORT_DESC,$follow,SORT_DESC,$good); echo '
';
print_r($good);
echo '
'; //方法二 function xx($a, $b) { if ($a['price'] == $b['price']) { return ($a['hot'] array('price'=>123,'hot'=>34543), 1=>array('price'=>434,'hot'=>234), 2=>array('price'=>42,'hot'=>2232), 3=>array('price'=>42,'hot'=>235432), 4=>array('price'=>33443,'hot'=>12), 4=>array('price'=>434,'hot'=>1211), ); usort($a, 'xx'); echo '
';
print_r($a);
echo '
';

>>> 更多内容,请查看 php数组排序方法大全



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn