PHP Array Function array_multisort () usage instance analysis, arraymultisort

Source: Internet
Author: User

PHP Array Function array_multisort () usage instance analysis, arraymultisort

This article analyzes the usage of the PHP Array Function array_multisort. We will share this with you for your reference. The details are as follows:

Sometimes we need to sort the values of a key in a two-dimensional array, which is discussed here. We can use the array_multisort () function. The array_multisort () function sorts multiple arrays or multi-dimensional arrays.

The array in the parameter is treated as a table column and sorted BY rows-similar to the SQL ORDER BY clause function. The first array is the main array to be sorted. If the rows (values) in the array are the same, they are sorted according to the corresponding values in the next input array.

The first parameter is an array, and each subsequent parameter may be an array or one of the following sort order signs (the sort mark is used to change the default sort order:

SORT_ASC-default, in ascending order. A-Z)
SORT_DESC-sort in descending order. Z-A)

You can then specify the sorting type:

SORT_REGULAR-default. Sort each item in the general order.
SORT_NUMERIC-Sort each item in numerical order.
SORT_STRING-Sort each item in alphabetical order.

Syntax: array_multisort (array1, sorting order, sorting type, array2, array3 ...)

Array1: required. Specifies the input array.
Sorting order: Optional. Specify the order. Possible values are SORT_ASC and SORT_DESC.
Sorting type: Optional. Specifies the sorting type. Possible values are SORT_REGULAR, SORT_NUMERIC, and SORT_STRING.
Array2: Optional. Specifies the input array.
Array3: Optional. Specifies the input array.

The string key will be retained, but the number key will be re-indexed, starting from 0 and increasing at 1. You can set the sorting order and type behind each array. If no value is set, the default value is used for each array parameter.

The following is an example:

<? Php $ arr = ''; echo 'two-dimensional array :'. '<br/>'; for ($ I = 0; $ I <= 5; $ I ++) {$ arr [$ I] ['val '] = mt_rand (1,100); $ arr [$ I] ['num'] = mt_rand (1,100 );} echo '<pre>'; print_r ($ arr); echo '</pre>'; echo 'extracts the key from the two-dimensional array as val, and separately creates another array :'. '<br/>'; foreach ($ arr as $ key => $ row) {$ vals [$ key] = $ row ['val']; $ nums [$ key] = $ row ['num'];} echo '<pre>'; print_r ($ vals); echo '</pre> '; echo 'sort it :'. '<br/>'; array_multisort ($ Vals, SORT_ASC, $ arr); echo '<pre>'; print_r ($ vals); echo '</pre>';?>

Running result:

Two-dimensional Array: Array ([0] => Array ([val] => 46 [num] => 49) [1] => Array ([val] => 8 [num] => 24) [2] => Array ([val] => 37 [num] => 3) [3] => Array ([val] => 32 [num] => 35) [4] => Array ([val] => 19 [num] => 38) [5] => Array ([val] => 30 [num] => 37) extract the key from the two-dimensional Array as val and separate it into another Array: array ([0] => 46 [1] => 8 [2] => 37 [3] => 32 [4] => 19 [5] => 30) sort it: array ([0] => 8 [1] => 19 [2] => 30 [3] => 32 [4] => 37 [5] => 46)

In this way, we will get a two-dimensional array in ascending order of val.

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.