Sort Multi-Dimensional Arrays


/ Published in: PHP
Save to your folder(s)

Very helpful function for sorting multi-dimensional arrays


Copy this code and paste it in your HTML
  1. // coded by alex dot hristov dot 88
  2. function sortmulti ($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) {
  3. if(is_array($array) && count($array)>0) {
  4. foreach(array_keys($array) as $key)
  5. $temp[$key]=$array[$key][$index];
  6. if(!$natsort) {
  7. if ($order=='asc')
  8. asort($temp);
  9. else
  10. arsort($temp);
  11. }
  12. else
  13. {
  14. if ($case_sensitive===true)
  15. natsort($temp);
  16. else
  17. natcasesort($temp);
  18. if($order!='asc')
  19. $temp=array_reverse($temp,TRUE);
  20. }
  21. foreach(array_keys($temp) as $key)
  22. if (is_numeric($key))
  23. $sorted[]=$array[$key];
  24. else
  25. $sorted[$key]=$array[$key];
  26. return $sorted;
  27. }
  28. return $sorted;
  29. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.