PHP array_multisort () function
Examples
It returns an array in ascending order:
<?php
$a=array("Dog","Cat","Horse","Bear","Zebra");
array_multisort($a);
print_r($a);
?>
$a=array("Dog","Cat","Horse","Bear","Zebra");
array_multisort($a);
print_r($a);
?>
Running instance »
Definition and Usage
array_multisort () function returns a sorted array. You can enter one or more arrays. Function to sort an array of first, followed by other arrays, if two or more of the same value, the next will sort the array.
Note: The string key name will be retained, but the number keys will be re-index, starting at 0 and increasing by 1.
Note: You can set the sort order and sort type parameters after each array.If not set, each array will use the default parameter values.
grammar
array_multisort( array1,sorting order,sorting type,array2,array3... )
参数 | 描述 |
---|---|
array1 | 必需。规定数组。 |
sorting order | 可选。规定排列顺序。可能的值:
|
sorting type | 可选。规定排序类型。可能的值:
|
array2 | 可选。规定数组。 |
array3 | 可选。规定数组。 |
technical details
return value: | If successful it returns TRUE, on failure returns FALSE. |
---|---|
PHP version: | 4+ |
Update log: | Sort Type SORT_NATURAL and SORT_FLAG_CASE is new in PHP 5.4 in. Sort Type SORT_LOCALE_STRING is new in PHP 5.3 in. |
More examples
Example 1
It returns an array in ascending order:
<?php
$a1=array("Dog","Cat");
$a2=array("Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
?>
$a1=array("Dog","Cat");
$a2=array("Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
?>
Running instance »
Example 2
When the two values are the same sort of how:
<?php
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
?>
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
?>
Running instance »
Example 3
Sort parameter:
<?php
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,SORT_ASC,$a2,SORT_DESC);
print_r($a1);
print_r($a2);
?>
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,SORT_ASC,$a2,SORT_DESC);
print_r($a1);
print_r($a2);
?>
Running instance »
Example 4
Merge two arrays, according to figures in descending order:
<?php
$a1=array(1,30,15,7,25);
$a2=array(4,30,20,41,66);
$num=array_merge($a1,$a2);
array_multisort($num,SORT_DESC,SORT_NUMERIC);
print_r($num);
?>
$a1=array(1,30,15,7,25);
$a2=array(4,30,20,41,66);
$num=array_merge($a1,$a2);
array_multisort($num,SORT_DESC,SORT_NUMERIC);
print_r($num);
?>
Running instance »
Complete PHP Array Reference