how to sort array which is the same with datebase data?
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
my array now is like:
$arr=array(
'header'=>array('name','age','money'),
'values'=>array(
array('jimmy',20,30),
array('tina',18,12),
array('darcy',19,50),
)
);
but now I had a large array not a db table , then how to sort the array ?
you can try array_multisort
: http://php.net/manual/en/function.array-multisort.php
foreach ($arr['values'] as $key => $row) {
$name[$key] = $row[0];
$age[$key] = $row[1];
$money[$key] = $row[3];
}
now if you want to sort by name in ASC you can:
array_multisort($name, SORT_ASC, $arr['values']);
or by name DESC:
array_multisort($name, SORT_DESC, $arr['values']);
or age ASC:
array_multisort($age, SORT_ASC, $arr['values']);
or age DESC and name ASC
array_multisort($age, SORT_DESC, $name, SORT_ASC, $arr['values']);
asort();
arsort();
krsort();
etc.. all are PHP inbuilt functions used to sort the array.
For better understanding you can visi the link http://php.net/manual/en/array.sorting.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With