Use array_multisort () in php

Source: Internet
Author: User
The array_multisort () of php uses this period of time for data statistics. The product requires that the data be sorted in descending order. if you think of many methods, none of them will work. Finally, find the PHP array_multisort () method. Although the results are achieved, this method is still

The following is an example of use. the examples in the manual are not listed, so I have several examples in my work.

The most common problem encountered at work is a two-dimensional array. I want to try a three-dimensional array, but forget it.

1 header ('content-Type: text/html; charset = utf-8 '); 2 echo'
'; 3 // original array format 4 $ array = array (5'key1' => array (6 'item1' => '65 ', 7 'item2' => '35', 8 'item3' => '84 ', 9 ), 10 'key2' => array (11 'item1' => '24', 12), 13 'key3' => array (14 'item1' => '38 ', 15 'item3' => '45', 16), 17 ); 18 // key 19 to be sorted // sort by item1 in the array 20 // You can also change to item221 $ sort = 'item1 '; 22 foreach ($ array as $ k => $ v) 23 {24 $ newArr [$ k] = $ v [$ sort]; 25} 26 // if this function is correctly executed, it will directly change the sequence of the original array key value 27 // if the execution fails, it will return bool (false) 28 array_multisort ($ newArr, SORT_DESC, $ array); 29 var_dump ($ array); 30 // --------------------- the print effect of the sorted array starts ---------------------- 31 array (3) {32 ["key1"] => 33 array (3) {34 ["item1"] => 35 string (2) "65" 36 ["item2"] => 37 string (2) "35" 38 ["item3"] => 39 string (2) "84" 40} 41 ["key3"] => 42 array (2) {43 ["item1"] => 44 string (2) "38" 45 ["item3"] => 46 string (2) "45" 47} 48 ["key2"] => 49 array (1) {50 ["item1"] => 51 string (2) "24" 52} 53} 54 // --------------------- the sorted array print effect ends ---------------------

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.