I have this multidimensional array, called $rent
:
Array
(
[product2] => Array
(
[dates] => Array
(
[2013-07-25] => 2
[2013-07-23] => 1
[2013-07-21] => 3
)
)
[product3] => Array
(
[dates] => Array
(
[2013-07-24] => 5
[2013-07-22] => 4
[2013-07-20] => 3
)
)
[product1] => Array
(
[dates] => Array
(
[2013-07-29] => 1
[2013-07-28] => 2
[2013-07-27] => 2
)
)
)
I'd like to do a double sort:
So that the resulting array would be:
Array
(
[product1] => Array
(
[dates] => Array
(
[2013-07-27] => 2
[2013-07-28] => 2
[2013-07-29] => 1
)
)
[product2] => Array
(
[dates] => Array
(
[2013-07-21] => 3
[2013-07-23] => 1
[2013-07-25] => 2
)
)
[product3] => Array
(
[dates] => Array
(
[2013-07-20] => 3
[2013-07-22] => 4
[2013-07-24] => 5
)
)
)
How can I reach this? Many thanks in advance
try this:
ksort($rent);
foreach($rent as &$item) {
ksort($item['dates']);
}
You can simply ksort the products, then iterate through them and use the same for the dates key.
ksort($products);
foreach($products as &$product)
ksort($product['dates']);
Where $products
is the array you showed us. Note that you need to pass the value in the foreach loop as a reference (using the &
operator) otherwise the changes won't be updated in the original array.
for my understanding of your problem; Nadh solution is almost there. but i believe you want ksort()
this is my corrections to Nadh answer
ksort($rent);
foreach($rent as $product => $dates) {
ksort($rent[$product]['dates']);
}
print_r($rent);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.