Home > Backend Development > PHP Tutorial > PHP two-dimensional array time sorting implementation code

PHP two-dimensional array time sorting implementation code

高洛峰
Release: 2023-03-03 13:30:01
Original
1400 people have browsed it

I discovered it yesterday when I was sorting the array. I wanted to sort by time, but PHP did not have this function built in, so I found this code online. The first parameter is the array, the second parameter is the element to be sorted, and the third parameter is the array. Each is the sorting method,

The following is the code for php two-dimensional array sorting

function arraySort($arr, $keys, $type = 'asc') {
    $keysvalue = $new_array = array();
    foreach ($arr as $k => $v){
      $keysvalue[$k] = $v[$keys];
    }
    $type == 'asc' ? asort($keysvalue) : arsort($keysvalue);
    reset($keysvalue);
    foreach ($keysvalue as $k => $v) {
      $new_array[$k] = $arr[$k];
    }
    return $new_array;
  }
$arr[] = array("name"=>"1","time"=>1) ;
$arr[] = array("name"=>"2","time"=>2);
arraySort($arr,"time","desc");
Copy after login


Related labels:
php
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