问题描述
如何对与日期数据库数据相同的数组进行排序?
how to sort array which is the same with datebase data?
我请求了Google Analytics(分析)数据,这些数据是一个大数组,我想将该数组与本地数据库中的其他字段合并,然后再次扩展大数组.现在,我想对使用我的sql的大型数组进行排序,如下所示:
I requested google analytics datas, the datas is an larget array , I want to join the array with some other fields from my local database , then I extend the large array again. Now I want to sort the large array which is the same with using my sql like this:
select * from ga_table where ...
select * from ga_table order by age
select * from ga_table group by name
我的数组现在就像:
$arr=array(
'header'=>array('name','age','money'),
'values'=>array(
array('jimmy',20,30),
array('tina',18,12),
array('darcy',19,50),
)
);
但是现在我有一个很大的数组而不是db表,那么如何对数组进行排序?
but now I had a large array not a db table , then how to sort the array ?
推荐答案
,您可以尝试array_multisort
: http://php.net/manual/zh/function.array-multisort.php
foreach ($arr['values'] as $key => $row) {
$name[$key] = $row[0];
$age[$key] = $row[1];
$money[$key] = $row[3];
}
现在,如果您要在ASC中按名称排序,则可以:
now if you want to sort by name in ASC you can:
array_multisort($name, SORT_ASC, $arr['values']);
或按名称DESC:
array_multisort($name, SORT_DESC, $arr['values']);
或年龄ASC:
array_multisort($age, SORT_ASC, $arr['values']);
或年龄DESC,并命名为ASC
or age DESC and name ASC
array_multisort($age, SORT_DESC, $name, SORT_ASC, $arr['values']);
这篇关于如何像mysql那样对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!