Home >Backend Development >PHP Tutorial >Sort 2D array by array value using php

Sort 2D array by array value using php

WBOY
WBOYOriginal
2016-07-25 08:41:54835browse
Multidimensional array sorting function code
  1. /**
  2. * Multi-dimensional array sorting
  3. * @param array $array Array to be sorted
  4. * @param string $key Sorting based on field
  5. * @param string $order Sorting mode, 0 is descending order, 1 is ascending order
  6. */
  7. function array_sort(array $array,$key,$order=1){
  8. $sort=[];
  9. // Form the field value here The corresponding relationship with the key name
  10. foreach($array as $k=>$v){
  11. $sort[$v[$key]]=isset($sort[$v[$key]])?array_merge($ sort[$v[$key]],[$k]):[$k];
  12. }
  13. if($order==1&&ksort($sort)){
  14. // Sort in ascending order
  15. }elseif($order= =0&&krsort($sort)){
  16. }else{
  17.                                                                     use using ’ ’ s ’       through using ’     through through through through ’s ’ through through through through through through's' ‐ to ‐ ‐‐‐‐ and � to foreach($value as $n){
  18.                                                                                                                                                                                                                                                                                 foreach($value as $n)
  19. php
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