Php (a small suspect in conditional sorting in a two-dimensional array)

Source: Internet
Author: User
Php (a small problem in sorting two-dimensional arrays by conditions) // The original Array ([1] = & gt; Array ([date] = & gt; [num] = & gt; 5) [2] = & gt; Array ([date] = & gt; 201 php (a small problem of conditional sorting in two-dimensional arrays)
// Original array
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 ))
// Method 1, $ ar = Array (1 => Array ('date' => '2017-08-18 ', 'num' => 5 ), 2 => Array ('date' => '2017-08-20 ', 'num' => 3), 3 => Array ('date' => '2017-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 ); // method 2, $ arr = array ('1' => array ('date' => '2017-08-18 ', 'num' => 5 ), '2' => array ('date' => '2017-08-20 ', 'num' => 3 ), '3' => array ('date' => '2017-08-17 ', 'num' => 10); foreach ($ arr as $ v) {$ tmp [$ v [date] = $ v;} ksort ($ tmp); print_r (array_values ($ tmp ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.