php多維陣列排序的例子

來源:互聯網
上載者:User
  1. $array = array(

  2. '0' => array('3', 'one'),
  3. '1' => array('101', 'two'),
  4. '2' => array('12', 'three'),
  5. '3' => array('13', 'four'),
  6. '4' => array('1', 'five'),
  7. '5' => array('3', 'six'),
  8. );

  9. /*方法一:使用的是uasort()函數,而不是usort()函數,區別在於,uasort()會保持原來的索引,usort()會重建索引。*/

  10. function cmp($a, $b){
  11. if($a[0] == $b[0]){
  12. return 0;
  13. }
  14. return($a[0] < $b[0]) ? -1 : 1;
  15. }

  16. uasort($array,"cmp");

  17. print_r($array);

  18. echo "
    **********************
    ";

  19. /*方法二: 使用array_multisort*/

  20. foreach ($array as $key=>$value){
  21. $first[$key] = $value[0];
  22. $second[$key] = $value[1];
  23. }
  24. array_multisort($first,SORT_NUMERIC,SORT_ASC,$second,SORT_STRING,SORT_ASC,$array);
  25. print_r($array);
  26. /*
  27. 排序次序標誌:
  28. SORT_ASC – 按照上升順序排序
  29. SORT_DESC – 按照下降順序排序

  30. 排序類型標誌:

  31. SORT_REGULAR – 將項目按照通常方法比較
  32. SORT_NUMERIC – 將項目按照數值比較
  33. SORT_STRING – 將項目按照字串比較
  34. */

複製代碼

>>> 更多內容,請查看 php數組排序方法大全 <<<

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.