PHP 2D array sorting

Source: Internet
Author: User

Introduction: This is a PHP two-dimensional array sorting details page, describes the knowledge, skills, experience, and some PHP Source Code related to PhP and PHP two-dimensional array sorting.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 358569 'rolling = 'no'>

PHP itself has a function for sorting multi-dimensional arrays.

Bool
Array_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 ordered by rows. This is 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 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. 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 ')
);
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. <textarea>Function multi_array_sort ($ multi_array, $ sort_key, $ sort = sort_asc) {<br/> If (is_array ($ multi_array) {<br/> foreach ($ multi_array as $ row_array) {<br/> If (is_array ($ row_array) {<br/> $ key_array [] = $ row_array [$ sort_key]; <br/>}else {<br/> return-1; <br/>}< br/>}} else {<br/> return-1; <br/>}< br/> array_multisort ($ key_array, $ sort, $ multi_array); <br/> return $ multi_array; <br/>}</textarea>

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/358569.html pageno: 1.

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.