php 二維數組按指定的索引值排序的代碼

來源:互聯網
上載者:User
  1. //二維數組 按索引值 排序
  2. function array_sort($array,$keys,$type='desc'){
  3. if(!isset($array) || !is_array($array) || empty($array)){
  4. return '';
  5. }
  6. if(!isset($keys) || trim($keys)==''){
  7. return '';
  8. }
  9. if(!isset($type) || $type=='' || !in_array(strtolower($type),array('asc','desc'))){
  10. return '';
  11. }
  12. $keysvalue=array();
  13. foreach($array as $key=>$val){
  14. $val[$keys] = str_replace('-','',$val[$keys]);
  15. $val[$keys] = str_replace(' ','',$val[$keys]);
  16. $val[$keys] = str_replace(':','',$val[$keys]);
  17. $keysvalue[] =$val[$keys];
  18. }
  19. asort($keysvalue); //key值排序
  20. reset($keysvalue); //指標重新指向數組第一個
  21. foreach($keysvalue as $key=>$vals) {
  22. $keysort[] = $key;
  23. }
  24. $keysvalue = array();
  25. $count=count($keysort);
  26. if(strtolower($type) != 'asc'){
  27. for($i=$count-1; $i>=0; $i--) {
  28. $keysvalue[] = $array[$keysort[$i]];
  29. }
  30. }else{
  31. for($i=0; $i<$count; $i++){
  32. $keysvalue[] = $array[$keysort[$i]];
  33. }
  34. }
  35. return $keysvalue;
  36. } //by bbs.it-home.org
  37. ?>
複製代碼

>>> 更多內容,請查看 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.