I have a multidimensional array like this one ( each $orders[$userid] array has multiple arrays of orders)
foreach($orders[$userid] as $order){
print_r($order);
}
This is the first user's orders grouped by date,month,year
Array ( [id] => 409079 [user_id] => 26017 [total] => 30 [final_total] => 29.1 [order_status_id] => 1
[day] => 1 [month] => 11 [year] => 2016 [amount2] => 2198.2999696731567 )
Array ( [id] => 410744 [user_id] => 26017 [total] => 175 [final_total] => 165 [order_status_id] => 1
[day] => 2 [month] => 11 [year] => 2016 [amount2] => 2619.799982070923 )
Array ( [id] => 412268 [user_id] => 26017 [total] => 300 [final_total] => 293 [order_status_id] => 1
[day] => 3 [month] => 11 [year] => 2016 [amount2] => 4413.400000572205 )
Array ( [id] => 405860 [user_id] => 26017 [total] => 10 [final_total] => 9.8 [order_status_id] => 1
[day] => 30 [month] => 10 [year] => 2016 [amount2] => 352.5999994277954 )
Array ( [id] => 407500 [user_id] => 26017 [total] => 85 [final_total] => 84.5 [order_status_id] => 1
[day] => 31 [month] => 10 [year] => 2016 [amount2] => 1135.1000022888184 )
As you see older dated are displayed after recent dates. The question is how to sort this multidimensional array to display old dates then recent dates like this
Array ( [id] => 405860 [user_id] => 26017 [total] => 10 [final_total] => 9.8 [order_status_id] => 1
[day] => 30 [month] => 10 [year] => 2016 [amount2] => 352.5999994277954 )
Array ( [id] => 407500 [user_id] => 26017 [total] => 85 [final_total] => 84.5 [order_status_id] => 1
[day] => 31 [month] => 10 [year] => 2016 [amount2] => 1135.1000022888184 )
Array ( [id] => 409079 [user_id] => 26017 [total] => 30 [final_total] => 29.1 [order_status_id] => 1
[day] => 1 [month] => 11 [year] => 2016 [amount2] => 2198.2999696731567 )
Array ( [id] => 410744 [user_id] => 26017 [total] => 175 [final_total] => 165 [order_status_id] => 1
[day] => 2 [month] => 11 [year] => 2016 [amount2] => 2619.799982070923 )
Array ( [id] => 412268 [user_id] => 26017 [total] => 300 [final_total] => 293 [order_status_id] => 1
[day] => 3 [month] => 11 [year] => 2016 [amount2] => 4413.400000572205 )
Please use code:
foreach ($orders as $key => $row) {
$year[$key] = $row['year'];
$month[$key] = $row['month'];
$day[$key] = $row['day'];
}
array_multisort($year, SORT_ASC, $month, SORT_ASC, $day, SORT_ASC, $orders);
0 comments:
Post a Comment