What we will introduce to you today is aboutThe following describes the array_multisort function in the manual:
Array_multisort () can be used to sort multiple PHP two-dimensional arrays at a time, or sort multi-dimensional arrays by one or more dimensions.
The Association (string) 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-similar to the SQL ORDER BY clause. 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.
From the manual, we can see that PHP 2D array sorting is based on the first array, and the subsequent order is adjusted. 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 to array_multisort as the first parameter.