Sample code of php sorting two-dimensional arrays by specified key

Source: Internet
Author: User
Sorting two-dimensional arrays by specified key values may be difficult for some new users. The following describes how to implement this function in php. The code is as follows:


Function array_sort ($ array, $ key ){
If (is_array ($ array )){
$ Key_array = null;
$ New_array = null;
For ($ I = 0; $ I <count ($ array); $ I ++ ){
$ Key_array [$ array [$ I] [$ key] = $ I;
}
Ksort ($ key_array );
$ J = 0;
Foreach ($ key_array as $ k => $ v ){
$ New_array [$ j] = $ array [$ v];
$ J ++;
}
Unset ($ key_array );
Return $ new_array;
} Else {
Return $ array;
}
}

PHP two-dimensional array key value sorting

In PHP, array_multisort () can be used to sort multiple arrays at a time, or to sort multi-dimensional arrays based on one or more dimensions. The associated key name remains unchanged, but the number key name is re-indexed. The input array is treated as a table column and sorted by rows. the first array is the main array to be sorted. If the rows (values) in the array are the same, they are sorted by the corresponding values in the next input array.

However, if the array to be sorted is a two-dimensional array, it needs to be sorted by the key value of the array. for example, the following two-dimensional array needs to be sorted by sort key name, then array_multisort () it cannot be implemented directly:

$ Data [5] = array ('Volume '=> 67, 'version' => 2); $ data [4] = array ('Volume' => 86, 'version' => 1); $ data [2] = array ('Volume '=> 85, 'version' => 6 ); $ data [3] = array ('Volume '=> 98, 'version' => 2); $ data [1] = array ('Volume' => 86, 'version' => 6); $ data [6] = array ('Volume '=> 67, 'version' => 7 ); // prepare the array foreach ($ data as $ k => $ v) {$ edition [] = $ v ['version'];} array_multisort ($ edition, SORT_ASC, $ data); print_r ($ data );

Output:

Array(  [0] => Array    (      [volume] => 86      [edition] => 1    )  [1] => Array    (      [volume] => 67      [edition] => 2    )  [2] => Array    (      [volume] => 98      [edition] => 2    )  [3] => Array    (      [volume] => 85      [edition] => 6    )  [4] => Array    (      [volume] => 86      [edition] => 6    )  [5] => Array    (      [volume] => 67      [edition] => 7    ))

In order not to destroy the original key, a sorting function is written, which only supports two-dimensional arrays.

/*** Sort by the size of a key value in the array, only two-dimensional arrays ** @ param array $ array sorting array * @ param string $ key value * @ param bool $ asc default forward order * @ return array sorted array */function arraySortByKey (array $ array, $ key, $ asc = true) {$ result = array (); // sort out the array foreach ($ array as $ k => & $ v) {$ values [$ k] = isset ($ v [$ key])? $ V [$ key]: '';} unset ($ v); // sort the key values to be sorted. $ asc? Asort ($ values): arsort ($ values); // rearrange the original array foreach ($ values as $ k => $ v) {$ result [$ k] = $ array [$ k];} return $ result ;}

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.