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

PHP implements two-dimensional array time sorting

墨辰丷
Release: 2023-03-28 14:20:01
Original
2188 people have browsed it

This article mainly introduces the PHP two-dimensional array time sorting implementation code. Friends who need it can refer to it.

I discovered it yesterday when I wanted to sort the array. I need to sort by time, but PHP does not have a built-in This function, so I found this code on the Internet. The first parameter is the array, the second is the element to be sorted, and the third 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


The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php How to use curl to simulate IP and source for access

How to display hexadecimal image data on the img tag of html in PHP

php Implement the method of deleting the specified element from the array

The above is the detailed content of PHP implements two-dimensional array time sorting. For more information, please follow other related articles on the PHP Chinese website!

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