array_multisort()和array_column()联用效果直接叠满,1+1>100
先来看下两个函数的介绍和用法
array_column():
一般模式,不需要其中字段作为id,只需要提取val值
php
<?php
// 可能从数据库中返回数组
$a = [
[
'id' => 5698,
'first_name' => 'Peter',
'last_name' => 'Griffin',
],
[
'id' => 4767,
'first_name' => 'Ben',
'last_name' => 'Smith',
],
[
'id' => 3809,
'first_name' => 'Joe',
'last_name' => 'Doe',
],
];
$last_names = array_column($a, 'last_name');
print_r($last_names);
//最简单模式
/*
Array( [0] => Griffin
[1] => Smith [2] => Doe
)
*/
?>
升级模式,需要其中字段作为id,另一字段作为val值
php
<?php
// 可能从数据库中返回数组
$a = [
[
'id' => 5698,
'first_name' => 'Peter',
'last_name' => 'Griffin',
],
[
'id' => 4767,
'first_name' => 'Ben',
'last_name' => 'Smith',
],
[
'id' => 3809,
'first_name' => 'Joe',
'last_name' => 'Doe',
],
];
$last_names = array_column($a, 'last_name' ,'id');
print_r($last_names);
//id作为key,last_name作为val
/*
Array( [5698] => Griffin
[4767] => Smith [3809] => Doe)
*/
array_multisort():
返回一个升序排列的数组:
php
<?php
$a1=array("Dog","Cat");
$a2=array("Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
/*返回结果
Array ( [0] => Cat [1] => Dog ) Array ( [0] => Missy [1] => Fido )
*/
?>
当两个值相同时如何排序:
php
<?php
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
/*返回结果
Array (
[0] => Cat
[1] => Dog
[2] => Dog
)
Array (
[0] => Missy
[1] =>
Fido [2] => Pluto
)
*/
?>
使用排序参数:
php
<?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);
/*返回结果
Array (
[0] => Cat
[1] => Dog
[2] => Dog
)
Array (
[0] => Missy
[1] => Pluto
[2] => Fido
)
*/
?>
合并两个数组,并按数字降序排列:
php
<?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);
/*返回结果
Array (
[0] => 66
[1] => 41
[2] => 30
[3] => 30
[4] => 25
[5] => 20
[6] => 15
[7] => 7
[8] => 4
[9] => 1
)
*/
?>
以上是对两个函数的介绍和使用方法举例
下面开始合体进化,直接直呼我滴个*:
首先扩展下array_multisort(),如果对多个数组进行排序,排序结果是所有的数组都按第一个数组的顺序进行排列
php
<?php
$a1 = ['c', 'b', 'a'];
$a2 = ['b', 'c', 'a'];
$a3 = ['b', 'a', 'c'];
array_multisort($a1,$a2,$a3);
print_r($a1);print_r('<br>');
print_r($a2);print_r('<br>');
print_r($a3);print_r('<br>');
/*输出结果
Array ( [0] => a [1] => b [2] => c )
Array ( [0] => a [1] => c [2] => b )
Array ( [0] => c [1] => a [2] => b )
*/
?>
然后开始合体:
1.将多维数组按照某一列的字段来进行排序
php
<?php
$data[] = array('volume' => 67, 'id' => 2);
$data[] = array('volume' => 86, 'id' => 1);
$data[] = array('volume' => 86, 'id' => 3);
$data[] = array('volume' => 85, 'id' => 6);
$data[] = array('volume' => 98, 'id' => 2);
$data[] = array('volume' => 86, 'id' => 6);
$data[] = array('volume' => 67, 'id' => 7);
array_multisort(array_column($data, 'volume'), SORT_DESC,$data);//按volume倒序排
/*输出结果
array(7) {
[0]=> array(2) {
["volume"]=> int(98) ["id"]=> int(2)
}
[1]=> array(2) {
["volume"]=> int(86) ["id"]=> int(1)
}
[2]=> array(2) {
["volume"]=> int(86) ["id"]=> int(3)
}
[3]=> array(2) {
["volume"]=> int(86) ["id"]=> int(6)
}
[4]=> array(2) {
["volume"]=> int(85) ["id"]=> int(6)
}
[5]=> array(2) {
["volume"]=> int(67) ["id"]=> int(2)
}
[6]=> array(2) {
["volume"]=> int(67) ["id"]=> int(7)
}
}
*/
?>
2.按volume倒序排,volume相同时,按id正序排
php
<?php
$data[] = array('volume' => 67, 'id' => 2);
$data[] = array('volume' => 86, 'id' => 1);
$data[] = array('volume' => 86, 'id' => 3);
$data[] = array('volume' => 85, 'id' => 6);
$data[] = array('volume' => 98, 'id' => 2);
$data[] = array('volume' => 86, 'id' => 6);
$data[] = array('volume' => 67, 'id' => 7);
array_multisort(array_column($data,'volume'), SORT_DESC, array_column($data,'id'), SORT_ASC, $data);//按volume倒序排,volume相同时,按id正序排
/*输出结果
array(7) {
[0]=> array(2)
{
["volume"]=> int(98) ["id"]=> int(2)
}
[1]=> array(2) {
["volume"]=> int(86) ["id"]=> int(1)
}
[2]=> array(2) {
["volume"]=> int(86) ["id"]=> int(3)
}
[3]=> array(2) {
["volume"]=> int(86) ["id"]=> int(6)
}
[4]=> array(2) {
["volume"]=> int(85) ["id"]=> int(6)
}
[5]=> array(2) {
["volume"]=> int(67) ["id"]=> int(2)
}
[6]=> array(2) {
["volume"]=> int(67) ["id"]=> int(7)
}
}
*/
?>
3.按照年龄从大到小的顺序排列,如果年龄相同就按照名字升序排序
php
<?php
$array = [
["age" => 20, "name" => "a"],
["age" => 21, "name" => "d"],
["age" => 22, "name" => "e"],
["age" => 20, "name" => "b"],
["age" => 25, "name" => "c"]
];
array_multisort(array_column($array,'age'),SORT_NUMERIC,SORT_DESC,array_column($array,'name'),SORT_STRING,SORT_ASC,$array);
print_r($array);
/*返回结果
Array
(
[0] => Array
(
[age] => 25
[name] => c
)
[1] => Array
(
[age] => 22
[name] => e
)
[2] => Array
(
[age] => 21
[name] => d
)
[3] => Array
(
[age] => 20
[name] => a
)
[4] => Array
(
[age] => 20
[name] => b
)
)
*/
?>
到此撒花.