PHP array_multisort Sorting and instance code for array

Source: Internet
Author: User
The Array_multisort () function in PHP can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional. This article explains how to use the Array_multisort function.

The Array_multisort () function returns a sorted array. You can enter one or more arrays. The function first sorts the first array, then the other arrays, and if two or more values are the same, it sorts the next array.

Note: The string key name is retained, but the numeric key name is re-indexed, starting at 0 and incrementing by 1.

Note: You can set the sort order and sort type parameters after each array. If not set, the default value is used for each array parameter.

Grammar

Array_multisort (array1,sorting order,sorting type,array2,array3 ...)

Parameter description

return value

Returns TRUE on success, or FALSE on failure.

Description

The Array_multisort () function sorts multiple arrays or multidimensional arrays.

The array in the parameter is treated as a column of a table and sorted by rows-this is similar to the function of the SQL ORDER by clause. The first array is the primary array to sort. If the rows (values) in the array are the same, they are sorted according to the size of the corresponding values in the next input array, and so on.

The first parameter is an array, and each subsequent argument may be an array, or it may be one of the following sort order flags (the sort flags are used to change the default order):

SORT_ASC-By default, in ascending order. (A-Z)
Sort_desc-sorted in descending order. (Z-A)

You can then specify the sort type:

Sort_regular-Default. Arranges each item in a regular order.
Sort_numeric-Sorts each item in numerical order.
Sort_string-Arranges each item alphabetically.

Example one:

Sorting multidimensional arrays

<?php $ar = Array (    array ("Ten", 11,100,100, "a"),    Array ("2", 3,1)),   array_multisort ($ar [0], Sort_ Asc,sort_string, $ar [1],sort_numeric,sort_desc]; Var_dump ($ar);? >

In this example, after sorting, the first array becomes "Ten", 100,100,11, "a" (as strings are sorted in ascending order). The second array will contain 1, 3, "2", 2, 1 (as a number in descending order).

Operation Result:

Array (2) {[0]=> Array (5) {  [0]=> string (2) "Ten"  [1]=> int (+)  [2]=> int (+)  [3]=> int (one)  [4]=> string (1) "A"} [1]=> Array (5) {  [0]=> int (1)  [1]=> int (3)  [2]=> string (1) "2"  [3 ]=> Int (2)  [4]=> Int (1)}}

Example two:

Case-insensitive alphabetical sorting

Sort_string and Sort_regular are both case-sensitive letters, and uppercase letters precede lowercase letters.
To make a case-insensitive sort, sort by a lowercase copy of the original array.

<?php $array =array (' Alpha ', ' Atomic ', ' Beta ', ' Bank '); $array _lowercase=array_map (' Strtolower ', $array); Array_multisort ($array _lowercase,sort_asc,sort_string, $array); Print_r ($array);? >

Operation Result:

Array (  [0] = Alpha  [1] = Atomic  [2] = = Bank  [3] = = Beta)

Thank you for reading, hope to help everyone, thank you for the support of this site!

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.