php多維陣列排序

來源:互聯網
上載者:User

標籤:style   blog   color   使用   2014   問題   

問題:

 

有這樣一個數組$arr:

[1]=> array(3) { [0]=> string(89) "........./r1822113797800000_AIK1_20140626_190411.mp3" [1]=> string(4) "AIK1" [2]=> string(16) "18221*********" }

 

[2]=> array(3) { [0]=> string(89) "........./r1822113797800000_AIK1_20140626_190422.mp3" [1]=> string(4) "AIK1" [2]=> string(16) "1822*********" }  
......

......

要求按照時間20140626_190411來排序

這個用到了php的多維陣列排序array_mutisort()函數,用法如下:

foreach($arr as $key=>$row)                        {                            preg_match("/[0-9]{4}_[0-9]{6}/",$row[0],$match);//使用正則表達從"........./r1822113797800000_AIK1_20140626_190422.mp3"中匹配出時間20140626_190422                            $date_for_sort[$key] = $match[0];//將時間作為排序的關鍵字                        }array_multisort($date_for_sort,SORT_ASC,$arr);//使用$date_for_sort作為關鍵字對數組$arr進行升序排序

這個函數相當的好用呀~

 數組內容為中文的話,需要編碼轉換為GBK或者GB2312形式,它們本身就使用拼音排序的,用到了iconv()函數:

foreach($arrbelong2 as $key=>$row)                        {                                                        $belong2_for_sort[$key] = iconv(‘UTF-8‘, ‘GB2312‘,$row[‘fullname‘]);                        } array_multisort($belong2_for_sort,SORT_ASC,$arrbelong2);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.