PHP Tutorial - PHP array_multisort() Function






Definition

array_multisort() sorts multiple related arrays at the same time, preserving the relationship between the arrays.

String keys will be maintained, but numeric keys will be re-indexed.

Syntax

PHP array_multisort() Function has the following syntax.

array_multisort(array1,sortOrder,sortType,array2,array3...)

Parameter

ParameterIs RequiredDescription
array1Required.Array to sort
sortOrderOptional.Sort Order.
sortTypeOptional.Type to use to compare elements.
array2Optional.Array to sort
array3Optional.Array to sort

Possible values for sortOrder:

ValueDescription
SORT_ASCDefault. Sort in ascending order (A-Z)
SORT_DESCSort in descending order (Z-A)

Possible values for sortType:

ValueDescription
SORT_REGULARDefault. Compare elements as ASCII value
SORT_NUMERICCompare elements as numeric values
SORT_STRINGCompare elements as string values
SORT_LOCALE_STRINGCompare elements as string, based on the current locale which can be changed using setlocale()
SORT_NATURALCompare elements as strings using "natural ordering" like natsort()
SORT_FLAG_CASECan be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively




Example 1

Using sorting parameters:


<?php
    $a1=array("P","J","H");
    $a2=array("PHP","Java","HTML");
    array_multisort($a1,SORT_ASC,$a2,SORT_DESC);
    print_r($a1);
    print_r($a2);
?>

The code above generates the following result.

Example 2

Merge two arrays and sort them as numbers, in descending order:


<?php
    $a1=array(1,3,5,7,9);
    $a2=array(4,3,2,1,6);
    $num=array_merge($a1,$a2);
    array_multisort($num,SORT_DESC,SORT_NUMERIC);
    print_r($num);
?>

The code above generates the following result.





Example 3

The following script stores book information in three related arrays. By passing all three arrays to array_multisort(), the arrays are all sorted according to the values in the first array:


<?PHP/* www.  j  ava 2 s . c  om*/
$authors = array( "Java", "PHP", "CSS", "HTML" ); 
$titles = array( "Learn Java from java2s.com", "Learn PHP from java2s.com", "Learn CSS from java2s.com", "Learn HTML from java2s.com" ); 
$pubYears = array( 2000, 2001, 2002, 2003 ); 

array_multisort( $authors, $titles, $pubYears ); 
print_r ( $authors ); 
print "\n";
print_r ( $titles ); 
print "\n";
print_r ( $pubYears );   
print "\n";
array_multisort( $titles, $authors, $pubYears );  
print_r ( $authors ); 
print "\n";
print_r ( $titles ); 
print "\n";
print_r ( $pubYears );   
print "\n";
?>

The code above generates the following result.

Example 4

Use array_multisort() to sort multidimensional arrays.

The function then sorts the array by the first element of each nested array, then by the second element of each nested array, and so on.

The order of the elements in the nested array is untouched.


<?php //from  ww w .  j a v a2 s  .  c om
     $myBooks = array( 
      array( 
        "title" =>  "Learn PHP from java2s.com", 
        "author" =>  "java2s.com", 
        "pubYear" =>  2000 
      ), 
      array( 
        "title" =>  "Oracle", 
        "author" =>  "java2s.com", 
        "pubYear" =>  1962 
      ), 
      array( 
        "title" =>  "Learn Java from java2s.com", 
        "author" =>  "JavaAuthor", 
        "pubYear" =>  2001 
      ), 
      array( 
        "title" =>  "Learn HTML from java2s.com", 
        "author" =>  "HTMLAuthor", 
        "pubYear" =>  2002 
      ), 
      array( 
        "title" =>  "Learn CSS from java2s.com", 
        "author" =>  "CSSAuthor", 
        "pubYear" =>  2003 
      ), 
     ); 
     array_multisort( $myBooks ); 
     print_r( $myBooks );
?>  

The code above generates the following result.