So I have an array:
Array
(
[0] => Array
(
[observationdate] => 2015-10-30
[country] => US
[town] => Coventry
)
[1] => Array
(
[observationdate] => 2015-10-01
[country] => US
[town] => York
)
[2] => Array
(
[observationdate] => 2015-10-02
[country] => US
[town] => York
)
[3] => Array
(
[observationdate] => 2015-10-02
[country] => US
[town] => York
)
[4] => Array
(
[observationdate] => 2015-10-10
[country] => US
[town] => York
)
[5] => Array
(
[observationdate] => 2015-10-30
[country] => US
[town] => York
)
)
I want to sort it like this:
Observation date ex 2015-10-30
Town = Conventry
[observationdate] => 2015-10-30
[country] => US
[town] => Coventry
Town = York
[observationdate] => 2015-10-30
[country] => US
[town] => York
Observation date ex 2015-10-02
Town = York
[observationdate] => 2015-10-02
[country] => US
[town] => York
First part is to rearange
foreach($arraything as $item) {
$items[$item['observationdate']][] = $item;
}
Which works fine:
I get an array arrange by date:
Array
(
[2015-10-30] => Array
(
[0] => Array
(
[observationdate] => 2015-10-30
[country] => US
[town] => Coventry
)
[1] => Array
(
[observationdate] => 2015-10-30
[country] => US
[town] => York
)
)
[2015-10-01] => Array
(
[0] => Array
(
[observationdate] => 2015-10-01
[country] => US
[town] => York
)
)
[2015-10-02] => Array
(
[0] => Array
(
[observationdate] => 2015-10-02
[country] => US
[town] => York
)
[1] => Array
(
[observationdate] => 2015-10-02
[country] => US
[town] => York
)
)
[2015-10-10] => Array
(
[0] => Array
(
[observationdate] => 2015-10-10
[country] => US
[town] => York
)
)
)
Now within the array arranged by dates I need to arrange it by towns
foreach($items AS $catid => $cat_items) {
echo '<h3>'.$catid.'</h3>';
foreach($cat_items AS $itema) {
$itemsa[$itema['town']][] = $itema;
}
echo "<pre>";
print_r($itemsa);
echo "</pre>";
}
Now here starts my problem, since here is for each, each time it adds to the my new array
2015-10-30
Array
(
[Coventry] => Array
(
[0] => Array
(
[observationdate] => 2015-10-30
[country] => US
[town] => Coventry
)
)
[York] => Array
(
[0] => Array
(
[observationdate] => 2015-10-30
[country] => US
[town] => York
)
)
)
and by the date
2015-10-10
Array
(
[Coventry] => Array
(
[0] => Array
(
[observationdate] => 2015-10-30
[country] => US
[town] => Coventry
)
)
[York] => Array
(
[0] => Array
(
[observationdate] => 2015-10-30
[country] => US
[town] => York
)
[1] => Array
(
[observationdate] => 2015-10-01
[country] => US
[town] => York
)
[2] => Array
(
[observationdate] => 2015-10-02
[country] => US
[town] => York
)
[3] => Array
(
[observationdate] => 2015-10-02
[country] => US
[town] => York
)
[4] => Array
(
[observationdate] => 2015-10-10
[country] => US
[town] => York
)
)
)
I get all entries added to my new array.
How to stop that ?