PHP two-dimensional array _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
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.

 
 
  1. 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:

 
 
  1. array('id'=>array(1,3,2),
  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:

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

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.

 
 
  1. functionmulti_array_sort($multi_array,$sort_key,$sort=SORT_ASC){
  2. if(is_array($multi_array)){
  3. foreach($multi_arrayas$row_array){
  4. if(is_array($row_array)){
  5. $key_array[]=$row_array[$sort_key];
  6. }else{
  7. return-1;
  8. }
  9. }
  10. }else{
  11. return-1;
  12. }
  13. array_multisort($key_array,$sort,$multi_array);
  14. return$multi_array;
  15. }

The above is a simple introduction to the two-dimensional PHP array, and I hope to help you.


...

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.