Sort the two-dimensional array and rerank the upper mark

Source: Internet
Author: User
Two-dimensional array sorting, and the subscript re-arranged as the question, I sorted a field, after sorting, found that the subscript also followed, hope that the subscript again, from 0 ~ Start sorting. how can this problem be solved ?, PHPcode $ fruits = array (& quot; d & quot; = & gt; & quot; lemon & quot;, & quot; a & quot; = & sort two-dimensional arrays, and the subscript is re-arranged.
For example, after sorting a field, I found that the subscript also followed. I want to re-rank the subscript from 0 ~ Start sorting. how can this problem be solved?

------ Solution --------------------
PHP code
$ Fruits = array ("d" => "lemon", "a" => "orange", "B" => "banana ", "c" => "apple"); ksort ($ fruits); print"
"; Var_dump ($ fruits );
------ Solution --------------------
PHP code
$ Ar = array ("10", 11,100,100, "a"), array (1, 2, "2", 3, 1 )); array_multisort ($ ar [0], SORT_ASC, SORT_STRING, $ ar [1], SORT_NUMERIC, SORT_DESC); // SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRINGprint"
"; Print_r ($ ar );
------ Solution --------------------
I think you need this --
Check the last parameter of the last code.


Example #4 sort database results


In this example, each unit in the data array represents a row in a table. This is a collection of typical database records.


The data in the example is as follows:


Volume | edition
------- + --------
67 | 2
86 | 1
85 | 6
98 | 2
86 | 6
67 | 7


All data is stored in an array named data. This is usually the result obtained from the database through a loop, such as mysql_fetch_assoc ().


$ Data [] = array ('Volume '=> 67, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 1 );
$ Data [] = array ('Volume '=> 85, 'version' => 6 );
$ Data [] = array ('Volume '=> 98, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 6 );
$ Data [] = array ('Volume '=> 67, 'version' => 7 );
?>


In this example, we will sort volume in descending order and edition in ascending order.


Now we have an array containing rows, but array_multisort () needs an array containing columns. Therefore, we use the following code to obtain and sort columns.


// Retrieve the column list
Foreach ($ data as $ key => $ row ){
$ Volume [$ key] = $ row ['Volume '];
$ Edition [$ key] = $ row ['version'];
}

// Sort data by volume in descending order and edition in ascending order
// Use $ data as the last parameter and sort it by a common key
Array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data );
?>


The data set is sorted as follows:


Volume | edition
------- + --------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7



------ Solution --------------------
Set $ t to the sorting result, and the subscript is not in ascending order.
Then $ t = array_values ($ t );
It can be converted into a subscript in ascending order.

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.