PHP Array function array_multisort () Usage example analysis, arraymultisort_php tutorial

Source: Internet
Author: User
Tags getting started with php shuffle sorts

PHP Array function array_multisort () Usage example analysis, Arraymultisort


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

Sometimes we need to sort the values of a key in a two-dimensional array, and this is where we discuss the problem. We can use the Array_multisort () function. 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.

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

Array1: Required. Specifies the input array.
Sorting order: Optional. Specify the order of arrangement. The possible values are SORT_ASC and Sort_desc.
Sorting type: Optional. Specifies the sort type. The 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 name is preserved, but the number key is re-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 = "; Echo ' Two-dimensional array is as follows: '. '
'; for ($i =0; $i <=5; $i + +) {$arr [$i] [' val '] = Mt_rand (1, +); $arr [$i] [' num '] = Mt_rand (1);} Echo '
'; Print_r ($arr); Echo '
'; Echo ' Extracts the key from the two-dimensional array to Val, which is separated into another array: '. '
'; foreach ($arr as $key = + $row) { $vals [$key] = $row [' Val ']; $nums [$key] = $row [' num ']; } Echo '
'; Print_r ($vals); Echo '
'; Echo ' Sorts it: '. '
'; Array_multisort ($vals, SORT_ASC, $arr); Echo '
'; Print_r ($vals); Echo '
';? >

Operation 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] =&G T Array    (      [val] = [      num] = [  4] = = Array    (      [val] = [      num]  [5] = = Array    (      [val] =      [num] = Notoginseng)    ) extracts the key from a two-dimensional array to Val, Separate into another array: array (  [0] = [  1] = 8  [2] = [  3] = [  4] =  [5] = 30 ) to sort it: Array (  [0] = 8  [1] = [2] = [3] = [  4] = [  5] + [+] = 46)

So we'll get a two-dimensional array sorted by Val Ascending.

More readers interested in PHP related content can view this site: "Summary of PHP operations and operator usage," PHP Network Programming Tips Summary, "PHP Basic Grammar Primer Tutorial", "PHP operation Office Document tips summary (including word,excel,access, ppt), "PHP Date and Time usage summary", "PHP primer for Object-oriented programming", "PHP String Usage Summary", "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Shuffle array values in PHP random sort function usage
    • Shuffle instance of PHP's scrambled array function implemented in JavaScript
    • Introduction to the use of shuffle () and Array_rand () random functions in the sequence of PHP array functions
    • Summary of common array function usage in PHP
    • PHP Array function array_key_exists () Summary
    • Create a new array using the array function in PHP
    • PHP uses the Array_slice function to get a random array or the first few data
    • PHP uses Array_search function to implement array lookup method
    • Example Summary of PHP arrays and explode functions
    • PHP uses the In_array function to check if a value exists in the array
    • PHP Array Correlation Function Summary
    • PHP uses the Gettimeofday function to return the current time and store it in an associative array.
    • PHP access to the last element of the array function end () usage
    • php function Shuffle () method analysis of several random elements in an array

http://www.bkjia.com/PHPjc/1117039.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117039.html techarticle php Array function array_multisort () Usage example analysis, arraymultisort this article analyzes the PHP array function array_multisort () usage. Share to everyone for your reference, as follows: There are ...

  • 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.