Talk about PHP array merging tips _php tutorial

Source: Internet
Author: User
Tags array sort
We may encounter the problem of PHP array merging when we use the PHP array, this article introduces the PHP array merging, the PHP array sorting. The PHP array ordering is actually sorting the PHP array. In the 1995 years, PHP2.0 released. The second edition is named PHP/FI (Form interpreter). Php/fi joined the support for mSQL, and has since established PHP's position in Dynamic web development.

By the end of 1996, there were 15,000 websites using PHP/FI, and in 1997 years the number of sites using PHP/FI was more than 50,000. In the 1997 years, the third edition of the development Plan began, the development team joined Zeev Suraski and Andi Gutmans, and the third edition was named PHP3. In the 2000, PHP4.0 was introduced, which added many new features. In this article, you sort the result set that the database queries. Database query results sometimes can not be used directly, such as MySQL with in statements out of the results, so need to sort the results in some way. It's time to sort the PHP array. To sort the results of a database see the following example: In this example, each cell in the data array represents a row in a table. This is a typical way of storing array data in a database. The data in the example is as follows:

 
  
  
  1. Volume | Edition
  2. -------+--------
  3. | 2
  4. | 1
  5. | 6
  6. 98 | 2
  7. | 6
  8. 67 |

The data is all stored in an array named data. This is usually done by looping the results from the database, such as MYSQL_FETCH_ASSOC () (In fact this function you can see with the MYSQL_FETCH_ASSOC () function, the specific difference you can look at the PHP manual about key differences).

 
 
  1. $data [] = Array (' volume ' => , ' edition ' => 2);
  2. $data [] = Array (' volume ' => , ' edition ' => 1);
  3. $data [] = Array (' volume ' => , ' edition ' => 6);
  4. $data [] = Array (' volume ' => 98, ' edition ' => 2);
  5. $data [] = Array (' volume ' => , ' edition ' => 6);
  6. $data [] = Array (' volume ' => , ' edition ' => 7);

In this example, the volume is sorted in descending order, and the edition in ascending order.

Now that you have an array with rows, but Array_multisort () requires an array of columns, so use the following code to get the columns and sort them.

 
  
  
  1. Get a list of columns
  2. foreach ($data as $key => $row) {
  3. $volume [$key] = $row [' volume '];
  4. $edition [$key] = $row [' Edition '];
  5. }
  6. Sort data in descending order according to volume, in ascending order of edition
  7. The $data as the last parameter, sorted by the Universal key
  8. Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);

The PHP array is in the sorted order and the results are as follows:

 
  
  
  1. Volume | Edition
  2. -------+--------
  3. 98 | 2
  4. | 1
  5. | 6
  6. | 6
  7. | 2
  8. 67 |

Actually there are many other ways to use it in the sort order, such as PHP's array of Arsort (), Asort (), Ksort (), Krsort (), Natsort (), Natcasesort (), Rsort (), Usort ( ), Array_multisort () and Uksort ().


http://www.bkjia.com/PHPjc/446497.html www.bkjia.com true http://www.bkjia.com/PHPjc/446497.html techarticle We may encounter the problem of PHP array merging when we use the PHP array, this article introduces the PHP array merging, the PHP array sorting. The PHP array sort is actually sorting the PHP array ...

  • Related Article

    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.