Home  >  Article  >  Backend Development  >  PHP two-dimensional array sorting custom function

PHP two-dimensional array sorting custom function

WBOY
WBOYOriginal
2016-07-25 08:52:21941browse
A custom function for sorting two-dimensional arrays in PHP. Please share it with friends who find it useful.

The code is as follows:

<?php
/**
* func: array_sort
* desc: php二维数组排序
* edit: bbs.it-home.org
*/
function array_sort($arr,$keys,$type='asc'){
   $keysvalue = $new_array = array();
       foreach ($arr as $k=>$v){
       $keysvalue[$k] = $v[$keys];
   }
   if($type == 'asc'){
       asort($keysvalue);
   }else{
       arsort($keysvalue);
   }
   reset($keysvalue);
       foreach ($keysvalue as $k=>$v){
       $new_array[$k] = $arr[$k];
   }
   return $new_array;
} 
?>

>>> For more information, please view the complete list of php array sorting methods



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