Home > Backend Development > PHP Tutorial > PHP two-dimensional array sorting function

PHP two-dimensional array sorting function

WBOY
Release: 2016-07-25 08:54:56
Original
1001 people have browsed it
  1. functionarray_sort($arr,$keys,$type='asc'){
  2. $keysvalue= $new_array= array();
  3. foreach($arras$k=>$v) {
  4. $keysvalue[$k] = $v[$keys];
  5. }
  6. if($type== 'asc'){
  7. asort($keysvalue);
  8. }else{
  9. arsort($keysvalue);
  10. }
  11. reset($keysvalue);
  12. foreach($keysvalueas$k=>$v){
  13. $new_array[$k] = $arr[$k];
  14. }
  15. return$new_array;
  16. }
Copy code Description of the three parameters of the

function: $arr: the array to sort $keys: Specify which key value to sort by $type: sorting method, ascending or descending order, the default is ascending order

This PHP function sorts a two-dimensional array according to the specified key value and returns the sorted array.

Example:

  1. $newArray= array_sort($array,'price');
Copy code


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template