Home > Backend Development > PHP Tutorial > php(二维数组中按条件排序的一个小疑点)

php(二维数组中按条件排序的一个小疑点)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 10:53:54
Original
888 people have browsed it

php(二维数组中按条件排序的一个小问题)
//原数组
Array ( [1] => Array ( [date] => 2011-08-18 [num] => 5 ) [2] => Array ( [date] => 2011-08-20 [num] => 3 ) [3] => Array ( [date] => 2011-08-17 [num] => 10 ) ) 

//方法一、$ar = Array ( 1 => Array ( 'date' => '2011-08-18', 'num' => 5 ),             2 => Array ( 'date' => '2011-08-20', 'num' => 3 ),             3 => Array ( 'date' => '2011-08-17', 'num' => 10 )          );function mysort($a, $b) {   $a = strtotime($a['date']);   $b = strtotime($b['date']);   if ($a == $b) return 0;   return ($a < $b) ? -1 : 1;}usort($ar, 'mysort');print_r($ar);//方法二、$arr=array ( '1' => array ( 'date' => '2011-08-18', 'num' => 5 ) ,'2' => array ( 'date' => '2011-08-20', 'num' => 3 ) ,'3' => array ( 'date' => '2011-08-17', 'num' => 10 ) )  ; foreach($arr as $v){      $tmp[$v[date]]=$v; } ksort($tmp); print_r(array_values($tmp));
Copy after login

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
Latest Issues
How to create an array within an array?
From 1970-01-01 08:00:00
0
0
0
php array
From 1970-01-01 08:00:00
0
0
0
Array to array
From 1970-01-01 08:00:00
0
0
0
php array rotation
From 1970-01-01 08:00:00
0
0
0
array
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template