abstrait:<?php //因?yàn)閡sort()函數(shù)針對(duì)的是多維數(shù)組,先定義一個(gè)多維數(shù)組 $arr=[ ['brand'=>'huawei','system'=>'android','price'=>6000], ['brand'=>'mi','system
<?php //因?yàn)閡sort()函數(shù)針對(duì)的是多維數(shù)組,先定義一個(gè)多維數(shù)組 $arr=[ ['brand'=>'huawei','system'=>'android','price'=>6000], ['brand'=>'mi','system'=>'android','price'=>5200], ['brand'=>'apple','system'=>'ios','price'=>9800], ['brand'=>'oppo','system'=>'android','price'=>3200] ]; //用usort()根據(jù)不同情況進(jìn)行比較 //根據(jù)品牌進(jìn)行排序 usort($arr,function($m,$n){ $a=$m['brand']; $b=$n['brand']; return strcmp($a,$b); //strcmp($b,$a) 將strcmp()中的參數(shù)互換位置就實(shí)現(xiàn)了降序 }); echo '<pre>'.var_export($arr,true),'<hr>'; //根據(jù)價(jià)格進(jìn)行排序 (只需更換數(shù)值中的鍵值即可) usort($arr,function($m,$n){ $a=$m['price']; $b=$n['price']; return strcmp($a,$b); //strcmp($b,$a) 將strcmp()中的參數(shù)互換位置就實(shí)現(xiàn)了降序 }); echo '<pre>'.var_export($arr,true),'<hr>'; ?>
Professeur correcteur:天蓬老師Temps de correction:2019-06-21 09:37:19
Résumé du professeur:其實(shí)php內(nèi)置了排序函數(shù)功能已經(jīng)非常強(qiáng)大, 但都是針對(duì)一維數(shù)組的, 對(duì)于多維數(shù)組, 就需要咱們自己動(dòng)手豐衣足食了