PHP Multidimensional Array sorting

Source: Internet
Author: User

Problem:

There is such an array $arr:

[1]=> Array (3) {[0]=> string (")" .../r1822113797800000_aik1_20140626_190411.mp3 "[1]=> string (4)" AIK1 "[2]=> string (16)" 18221********* "}

[2]=> Array (3) {[0]=> string (")" .../r1822113797800000_aik1_20140626_190422.mp3 "[1]=> string (4)" AIK1 "[2]=> string (16)" 1822********* "}
......

......

Required to be sorted by time 20140626_190411

This uses PHP's multidimensional array sort Array_mutisort () function, as follows:

foreach($arr  as $key=$row)                        {                            Preg_match("/[0-9]{4}_[0-9]{6}/",$row[0],$match)///Use regular expression to match time from ".../r1822113797800000_aik1_20140626_190422.mp3" 20140626_190422                             $date _for_sort[$key] =$match[0];//time as a sort of keyword}Array_multisort($date _for_sort, SORT_ASC,$arr);//Use$date _for_sort in ascending order as a keyword for an array $arr

This function is quite useful ~

If the contents of the array are in Chinese, they need to be converted into GBK or GB2312 forms, which themselves use phonetic sorting and use the Iconv () function:

foreach ($arrbelong 2as$key=$row)                        {                                                        $belong 2_for_sort[$keyiconv(' UTF-8 ', ' GB2312 ',$row[' FullName ']);                         Array_multisort ($belong 2_for_sort, SORT_ASC,$arrbelong 2);

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.