Home >Backend Development >PHP Tutorial >Example of sorting a php two-dimensional array by a specified key value

Example of sorting a php two-dimensional array by a specified key value

WBOY
WBOYOriginal
2016-07-25 08:55:411090browse
  1. /**
  2. * PHP two-dimensional array is sorted by the specified key value key
  3. * by bbs.it-home.org
  4. */
  5. function array_sort($array, $key){
  6. if(is_array($array)){
  7. $key_array = null;
  8. $new_array = null;
  9. for( $i = 0; $i < count( $array ); $i++ ){
  10. $key_array[$array[$i][$key]] = $i;
  11. }
  12. ksort($key_array);
  13. $j = 0;
  14. foreach($key_array as $k => $v){
  15. $new_array[$j] = $array[$v];
  16. $j++;
  17. }
  18. unset($key_array);
  19. return $new_array;
  20. }else{
  21. return $array;
  22. }
  23. }
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn