PHP two-dimensional array. PHP has been developing for a long time and many users are familiar with it. I have collected some knowledge about PHP two-dimensional arrays online. I would like to share with you here, PHP itself has a lot of PHP after a long period of development, many users know it very well, and some knowledge about the PHP two-dimensional array is collected online, here to share with you, PHP itself has a function for sorting multi-dimensional arrays.
- boolarray_multisort(array$ar1[,mixed$arg[,mixed$...[,array$...]]])
The following describes the array_multisort function in the manual:
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 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 ORDERBY clause of SQL. 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 the multi-dimensional sorting of PHP itself 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. But most of the time, the PHP two-dimensional array we constructed is like this:
- array(
- array('id'=>1,'data'=>'a'),
- array('id'=>3,'data'=>'c'),
- array('id'=>2,'data'=>'b')
- );
The elements of the array 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.
- functionmulti_array_sort($multi_array,$sort_key,$sort=SORT_ASC){
- if(is_array($multi_array)){
- foreach($multi_arrayas$row_array){
- if(is_array($row_array)){
- $key_array[]=$row_array[$sort_key];
- }else{
- return-1;
- }
- }
- }else{
- return-1;
- }
- array_multisort($key_array,$sort,$multi_array);
- return$multi_array;
- }
The above is a simple introduction to the two-dimensional PHP array, and I hope to help you.
...