array("10", 11, 100, 100, "a"),
array( 1, 2, "2", 3, 1)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC) ;
var_dump($ar);
?>
Result:
array
0 =>
array
0 =>
string
'10' (length=2)
1 =>
int
100
2 =>
int
100
3 =>
int
11
4 =>
string
'a' (length=1)
1 =>
array
0 =>
int
1
1 =>
int
3
2 =>
string
'2' (length=1)
3 =>
int
2
4 =>
int
1
//Explanation:
1 In the above example: the $ar array is first sorted in ascending order according to the string value of $ar[0]. If the string values are equal, then the numbers in the $ar[1] array are sorted. Values are sorted in descending order.
2 If the parameter at any position of the array_multisort function is an array, it represents the value used for sorting.
If there are multiple array parameters, the previous array value will be sorted first. If it is a constant, such as
SORT_ASC, SORT_DESC, SORT_REGULAR,SORT_NUMERIC, SORT_STRING.
indicates the sorting method (array values are taken first).
================================================== ===========================================
PHP Two-dimensional array sorting function
PHP one-dimensional array can be sorted using functions such as sort(), asort(), arsort(), etc., but the sorting of PHP two-dimensional array needs to be customized.
The following function sorts a given two-dimensional array according to the specified key value. Let’s look at the function definition first:
Copy the code The code is as follows:
function array_sort($arr,$keys,$type='asc'){
$keysvalue = $new_array = array();
foreach ($arr as $k= >$v){
$keysvalue[$k] = $v[$keys];
}
if($type == 'asc'){
asort($keysvalue);
}else{
arsort($keysvalue);
}
reset($keysvalue);
foreach ($keysvalue as $k=>$v){
$new_array [$k] = $arr[$k];
}
return $new_array;
}
It can sort the two-dimensional array according to the specified key value, You can also specify ascending or descending order (default is ascending order), usage example:
Copy code The code is as follows:
$ array = array(
array('name'=>'Mobile phone','brand'=>'Nokia','price'=>1050),
array('name'=>' laptop','brand'=>'lenovo','price'=>4300),
array('name'=>'razor','brand'=>'Philips', 'price'=>3100),
array('name'=>'Treadmill','brand'=>'Sanwa Soushi','price'=>4900),
array ('name'=>'Watch','brand'=>'Casio','price'=>960),
array('name'=>'LCD TV','brand'= >'Sony','price'=>6299),
array('name'=>'Laser Printer','brand'=>'HP','price'=>1200)
);
$ShoppingList = array_sort($array,'price');
print_r($ShoppingList);
The above is the two-dimensional array of $array according to 'price' 'Sort from low to high.
Output result: (omitted).
http://www.bkjia.com/PHPjc/327874.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327874.htmlTechArticleThe sorting of several array functions mentioned below have some commonalities: 1 The array is used as a parameter of the sorting function, sorting Later, the array itself has changed, and the return value of the function is of type bool. ...