這篇文章給大家介紹怎么在PHP中根據(jù)鍵值對(duì)二維數(shù)組進(jìn)行排序,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
問(wèn)題:
數(shù)組:
array(0=>array('id'=>1,'price'=>50),1=>array('id'=>2,'price'=>60));
要求根據(jù)數(shù)組的price這個(gè)字段進(jìn)行排序。
實(shí)現(xiàn)代碼如下:
<?php $array[] = array('id'=>1,'price'=>50); $array[] = array('id'=>2,'price'=>70); $array[] = array('id'=>3,'price'=>30); $array[] = array('id'=>4,'price'=>20); foreach ($array as $key=>$value){ $id[$key] = $value['id']; $price[$key] = $value['price']; } array_multisort($price,SORT_NUMERIC,SORT_DESC,$id,SORT_STRING,SORT_ASC,$array); echo '<pre>'; print_r($array); echo '</pre>'; ?>
運(yùn)行結(jié)果:
Array ( [0] => Array ( [id] => 2 [price] => 70 ) [1] => Array ( [id] => 1 [price] => 50 ) [2] => Array ( [id] => 3 [price] => 30 ) [3] => Array ( [id] => 4 [price] => 20 ) )
關(guān)于怎么在PHP中根據(jù)鍵值對(duì)二維數(shù)組進(jìn)行排序就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站標(biāo)題:怎么在PHP中根據(jù)鍵值對(duì)二維數(shù)組進(jìn)行排序-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://chinadenli.net/article32/dsicsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站營(yíng)銷(xiāo)、手機(jī)網(wǎng)站建設(shè)、微信小程序、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容