Two-dimensional array sorting, and superscript rearrangement

Source: Internet
Author: User
Two-dimensional array sorting, and subscript rearrangement
Title, I sort of a field, row, found subscript also follow, hope Subscript re-arrange, from 0~ start Platoon, ask how to do?

------Solution--------------------
PHP Code
$fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple"), Ksort ($fruits);p rint "
"; Var_dump ($fruits);
------Solution--------------------
PHP Code
$ar = Array (Array ("Ten", One, one, 1, "a"), and an array (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--
Take a look at the last parameter of the last piece of code


Example #4 Sorting the results of a database


In this example, each cell in the data array represents a row in a table. This is a data 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


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 ().


$data [] = Array (' volume ' = +, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 1);
$data [] = Array (' volume ' = =, ' edition ' = 6);
$data [] = Array (' volume ' = = 98, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 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.


Get a list of columns
foreach ($data as $key = = $row) {
$volume [$key] = $row [' volume '];
$edition [$key] = $row [' Edition '];
}

Sort data in descending order according to volume, in ascending order of edition
The $data as the last parameter, sorted by the Universal key
Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);
?>


The data set is now sorted and the results are as follows:


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



------Solution--------------------
Set $T is the result of a sort, subscript is not ascending
Then $t = Array_values ($t);
It can be converted to subscript ascending.
  • 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.