PHP array_multisort() Function

In this chapter you will learn:

  1. Definition for PHP array_multisort() Function
  2. Syntax for PHP array_multisort() Function
  3. Parameter for PHP array_multisort() Function
  4. Example - Using sorting parameters
  5. Example - Merge two arrays and sort them as numbers, in descending order
  6. Example - array_multisort keeps the relationship between arrays
  7. Example - Use array_multisort() to sort multidimensional arrays

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/*from  java 2  s  . com*/
    $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/*from  j  a  v  a  2 s . c om*/
    $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/*j  av a2 s  .  c  o  m*/
$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  ja va 2  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.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP array_pad() Function
  2. Syntax for PHP array_pad() Function
  3. Parameter for PHP array_pad() Function
  4. Example - Return 5 elements and insert a value of "blue" to the new elements in the array
  5. Example - Using a negative size parameter
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function