+

PHP Array_multisort () function

Syntax:
bool array_multisort ($array1, sorting_order, sorting_type, $array2 ..)
Parameters:An array usually takes one parameter, which is the array to be sorted. But in addition, the function can take two more optional parameters sorting_order and sorting_type.
  • $array1 : This parameter specifies the array we want to sort.
  • sorting_order : This parameter specifies the order of use, i.e. in ascending or descending order. The default value for this parameter is SORT_ASC. That is, sorting in ascending order. To sort in descending order, we need to set this parameter to SORT_DESC.
  • sorting_type : This parameter specifies the sorting options for arrays, and they are as follows:
    • SORT_REGULAR: compare items regularly (ASCII standard).
    • SORT_NUMERIC: compare items as numeric values.
    • SORT_STRING: compare items as string values.
    • SORT_LOCALE_STRING: compare items as a string based on the current locale.
    • SORT_NATURAL: compare items as strings using "natural ordering".
    • SORT_FLAG_CASE: can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings are case insensitive.
  • If we want to sort multiple arrays, we can pass them as parameters such as $array2, $array3 ... and then sorting_order, sorting_type . Return value:array_multisort() function return increases boolean value. That is, it will return TRUEon success and FALSEon failure.Note.If two terms become equal, their relative order in the sorted array is undefined.The following programs illustrate the array_multisort() function:Program 1:  
    // input array $animals = array ( "Dog" , "Cat" , "Horse" , "Bear" ,
    "Zebra" , "Lion " );  
    // sort array by default
    // for sorting_order and sorting_type array_multisort (< / code> $animals ); print_r ( $animals );  
    ?>
    Output:
    Array ([ 0] = > Bear [1] = > Cat [2] = > Dog [3] = > Horse [4] = > Lion [5] = > Zebra)
    Program 2:  
    // Input arrays $array1 = array ( "Dog" , "Cat" ); $array2 = array ( "Fido" , "Missy" );  
    // sort multiple arrays by default
    // for sorting_order and sorting_type array_multisort ( $array1 , $array2 );  
    // print sorted arrays print_r ( $array1 ); print_r ( $array2 );  
    ?>
    Output:
    Array ([ 0] = > Cat [1] = > Dog) Array ([0] = > Missy [1] = > Fido)
    Program 3:  
    // Input arrays $array1 = array ( "Dog" , "Dog" , "Cat" ); $array2 = array ( "Pluto" , "Fido" , "Missy" );  
    // sort multiple arrays array_multisort ( $array1 , SORT_ASC, $array2 , SORT_DESC);  
    // Print sorted arrays print_r ( $array1 ); print_r ( $array2 );  
    ?>
    Output:
    Array ([ 0] = > Cat [1] = > Dog [2] = > Dog) Array ([0] = > Missy [1] = > Pluto [2] = > Fido)
    Link :
    http://php.net/manual/en /function.array-multisort.php
    Get Solution for free from DataCamp guru