Home >Backend Development >PHP Tutorial >How to implement array sorting by specified KEY in PHP_PHP Tutorial
This article mainly introduces the method of sorting arrays in php by specified KEY. It involves the skills of sorting arrays in php. It has certain For reference value, friends in need can refer to it
The example in this article describes how PHP implements array sorting by specified KEY. Share it with everyone for your reference. The specific implementation method is as follows:
?
3 4 513 14
15
16
|
function array_sort($arr,$keys,$orderby='asc'){ $keysvalue = $new_array = array(); foreach ($arr as $k=>$v){ $keysvalue[$k] = $v[$keys]; } if($orderby== 'asc'){ asort($keysvalue); }else{ arsort($keysvalue); } reset($keysvalue); foreach ($keysvalue as $k=>$v){ $new_array[] = $arr[$k]; } return $new_array; } |