PHP Array_multisort The sorting of the array and instance code _php instance

Source: Internet
Author: User
Tags lowercase sorts

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. function first sorts the first array, then the other array, and if two or more values are the same, it sorts the next array.

Note: The string key name is preserved, but the numeric key name is indexed, starting at 0 and incremented 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

Parameters Description
Array1 Necessary. The one to sort.
Sorting order

Optional. Specify the order of arrangement. Possible values:

  • SORT_ASC-Default. In ascending order (A-Z).
  • Sort_desc-In descending order (Z-A).
Sorting type

Optional. Specify the sort type. Possible values:

  • Sort_regular-Default. Arrange each item in a regular order (Standard ASCII without changing the type).
  • Sort_numeric-Treat each item as a number.
  • Sort_string-Treat each item as a string.
  • Sort_locale_string-handles each item as a string, based on the current locale (which can be changed by setlocale ()).
  • Sort_natural-handles each item as a string, using a natural sort similar to Natsort ().
  • Sort_flag_case-The string can be sorted in combination (bitwise OR) sort_string or sort_natural, case-insensitive.
Array2 Optional. The specified array.
Array3 Optional. The specified array.

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 argument is treated as a column of a table and sorted by row-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 compare to the same, they are sorted according to the size of the corresponding values in the next input array, and so on.

The first argument 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 arrangement order):

SORT_ASC-Default, in ascending order. (A-Z)
Sort_desc-in descending order. (Z-A)

You can then specify the sort type:

Sort_regular-Default. Arranges each item in a regular order.
Sort_numeric-Arranges each item in numerical order.
Sort_string-Arranges each entry in alphabetical order.

Example one:

Sort multidimensional arrays

<?php
 $ar = array (
    array ("Ten", 11,100,100, "a"),
    Array (1,2, "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, and "a" (sorted in ascending order as a string). The second array will contain 1, 3, "2", 2, 1 (sorted as numbers in descending order).

Run Result:

Array (2) {
 [0]=> Array (5) {
  [0]=> string (2) "Ten"
  [1]=> int (m)
  [2]=> Int (m)
  [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

Both sort_string and Sort_regular are case-sensitive and uppercase letters precede lowercase letters.
To do a case-insensitive sort, sort by the lowercase letter 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);
? > 

Run Result:

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

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.