PHP Array function array_multisort () Usage example Analysis _php technique

Source: Internet
Author: User

This paper analyzes the use of PHP array function array_multisort (). Share to everyone for your reference, specific as follows:

Sometimes we need to sort the values of a key in a two-dimensional array, and here's the question. We can use the Array_multisort () function. 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.

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

Array1: Required. A specified array of inputs.
Sorting order: Optional. Specify the order of arrangement. The possible values are SORT_ASC and Sort_desc.
Sorting type: Optional. Specify the sort type. The possible values are Sort_regular, Sort_numeric, and sort_string.
Array2: Optional. A specified array of inputs.
ARRAY3: Optional. A specified array of inputs.

The string key name is preserved, but the number key is indexed, starting at 0 and incrementing by 1. You can set the sort order and sort type after each array. If not set, the default value is used for each array parameter.

Here is an example:

<?php
 $arr = ';
 The echo ' two-dimensional array is as follows: '. ' <BR/> ';
 For ($i =0 $i <=5; $i + +)
 {
 $arr [$i] [' val '] = Mt_rand (1);
 $arr [$i] [' num '] = Mt_rand (1);
 }
 Echo ' <pre> ';
 Print_r ($arr);
 Echo ' </pre> ';
 echo extracts the key from the two-dimensional array to Val and separate it into 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> ';
? >

Run Result:

The two-dimensional array is as follows: Array
(
  [0] => array
    (
      [Val] =>
      [num] =>
    )
  [1] => array
    (
      [Val] => 8
      [num] =>
  [2] => Array
    (
      [Val] => notoginseng
      [num] => 3
    )
  [3] => array
    (
      [Val] =>
      [num] =>
    )
  [4] => array
    (
      [val] = >
      [num] =>
  [5] => Array
    (
      [Val] =>
      [num] =>
) c34/> extracts the key from the two-dimensional array as Val, and is separate into another array: array
(
  [0] =>
  [1] => 8
  [2] => Notoginseng
  [3] => 32< C41/>[4] =>
  [5] =>
)

sort it:

Array
(
  [0] => 8
  [1] =>
  [2] = >
  [3] => [
  4] => Notoginseng
  [5] =>


So we'll get a two-dimensional array sorted by Val in ascending order.

More interested in PHP related content readers can view the site topics: "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access, PPT), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Course", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.