PHP sorts the two arrays by the value of an element in the second dimension, and php sorts the two arrays.

Source: Internet
Author: User

PHP sorts the two arrays by the value of an element in the second dimension, and php sorts the two arrays.

For example:

1 // The original array is like this, and you want to sort it in ascending or descending order of run_date In the second dimension: 2 $ arr = array (3 0 => array (4 'run _ date' => '2017-11-21 ', 5 'Count' => '5' 6 ), 7 1 => array (8 'run _ date' => '2017-11-20 ', 9 'Count' => '10' 10 ), 11 2 => array (12 'run _ date' => '2017-11-22 ', 13 'Count' => '10' 14) 15); 16 // You Want To get: 17 $ arr = array (18 0 => array (19 'run _ date' => '2017-11-20 ', 20 'Count' => '5' 21 ), 22 1 => array (23 'run _ date' => '2017-11-21 ', 24 'Count' => '10' 25 ), 26 2 => array (27 'run _ date' => '2017-11-22 ', 28 'Count' => '10' 29) 30 );

Of course, loop traversal can solve the problem, but I still think the efficiency is a little low. Here is another solution that uses the functions provided by two PHP:

Array_column,Array_multisort

Or the above array:
// First use the array_column multi-dimensional array to retrieve $ date = array_column ($ arr, 'Run _ date') according to the vertical (column); // The above result is obtained: array (0 => '2017-11-21 ', 1 => '2017-11-20', 3 => '2017-11-22 ') // sort $ arr by array_multisort ($ date, SORT_ASC, $ arr) with the result of array_column; // the final result is as follows: $ arr = array (0 => array ('run _ date' => '2017-11-20 ', 'Count' => '5 '), 1 => array ('run _ date' => '2017-11-21 ', 'Count' => '10 '), 2 => array ('run _ date' => '2017-11-22 ', 'Count' => '10 '));

Appendix:

1,Array_columnRefer to my previous blog http://www.cnblogs.com/Steven-shi/p/5897745.html

2,Array_multisortThe array_multisort () function sorts multiple arrays or multi-dimensional arrays. It can be used to sort multiple PHP two-dimensional arrays at a time, or sort multidimensional arrays by one or more dimensions.

The array in the parameter is treated as a table column and sorted BY rows-similar to the SQL ORDER BY clause function. The first array is the main array to be sorted. If the rows (values) in the array are the same, they are sorted according to the corresponding values in the next input array.

The first parameter is an array, and each subsequent parameter may be an array or one of the following sort order signs (the sort mark is used to change the default sort order:

SORT_ASC-default, in ascending order. A-Z)
SORT_DESC-sort in descending order. Z-A)
You can then specify the sorting type:

SORT_REGULAR-default. Sort each item in the general order.
SORT_NUMERIC-Sort each item in numerical order.
SORT_STRING-Sort each item in alphabetical order.
Array_multisort (array1, sorting order, sorting type, array2, array3 ...)
Array1: required. Specifies the input array.

Sorting order: Optional. Specify the order. Possible values are SORT_ASC and SORT_DESC.

Sorting type: Optional. Specifies the sorting type. Possible values are SORT_REGULAR, SORT_NUMERIC, and SORT_STRING.

Array2: Optional. Specifies the input array.

Array3: Optional. Specifies the input array.

Note: The string key will be retained, but the number key will be re-indexed, starting from 0 and increasing at 1.

Note: You can set the sorting order and type behind each array. If no value is set, the default value is used for each array parameter.

PHP two-dimensional array sorting is to sort by the first array and adjust the subsequent order. Arrays like this:

array( 'id' => array(1,3,2), 'data'=>array('a','c','b'))

You only need to sort by id in multiple dimensions. However, in many cases, the arrays we construct are as follows:

array( array('id'=>1,'data'=>'a'), array('id'=>3,'data'=>'c'), array('id'=>2,'data'=>'b') );

Elements in the PHP two-dimensional array sorting are arranged in rows and must be sorted by one of the columns. PHP does not seem to provide functions similar to matrix transpose. Therefore, you cannot directly use array_multisort for multi-dimensional sorting. However, you only need to extract the sorted columns and pass them as the first parameter to array_multisort.

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.