Reputation: 13
I need some help. It seems so simple, but I can't get it done right. Here's the structure of my XML
<events>
<Show ID="1">
<Time>2013-01-05 17:30:00</Time>
</Show>
<Show ID="2">
<Time>2013-01-05 20:30:00</Time>
</Show>
<Show ID="3">
<Time>2013-01-06 17:30:00</Time>
</Show>
<Show ID="4">
<Time>2013-01-06 20:30:00</Time>
</Show>
<Show ID="5">
<Time>2013-01-06 21:30:00</Time>
</Show>
<Show ID="6">
<Time>2013-01-07 15:30:00</Time>
</Show>
</events>
Now what I need to do, is to split up the "Time" Element into two values: Date and Time:
<?php $xml = simplexml_load_file('file.xml');
foreach ( $xml->Show as $Show )
{
// ...
foreach ( $Show->Time as $Date )
{
$Timecon = date_create_from_format('Y-m-d H:i:s', $Date);
$day = date_format($Timecon, 'd.m.');
$hours = date_format($Timecon, 'H:i');
}
}
?>
I spare you the rest of the code, because I actually scrapped it - it was wrong anyways, plus I'm a noob: Formatting the $Date into $day and $hours works perfectly fine, but as to grouping - whatever I do, this is what I get:
05.01. 17:30
05.01. 20:30
06.01. 17:30
06.01. 20:30
06.01. 21:30
07.01. 15:30
and this is what I need, because the XML can get quite long:
05.01. 17:30 20:30
06.01. 17:30 20:30 21:30
07.01. 15:30
As you can see I want the $hours to be grouped by $day or rather have every duplicate-$day skipped/deleted. I would prefer to do this with PHP, not with XSL.
And yes, I googled for hours and feel really stupid for not getting it. Thanks for you help!
Upvotes: 0
Views: 74
Reputation: 13
I used Chris' suggestion with a minor change and it works, thank you!
$group = array();
foreach ( $xml->Show as $Show ) {
foreach( $Show->Time as $Date ) {
$_date = new DateTime( $Date );
//if this is a new day, add it to the group
if( ! isset($group[$_date->format('m.d')]) )
$group[ $_date->format('m.d') ] = array();
//add the time to the array, organized by month.day
$group[ $_date->format('m.d') ][] = $_date->format('H:i');
}
}
foreach( $group as $month => $times ) { echo $month . ': <br />';
foreach( $times as $time ) { echo $time . '<br />'; }
}
}
now it will look like this
Array
(
[01.05] => Array
(
[0] => 17:30
[1] => 20:30
)
[01.06] => Array
(
[0] => 20:30
[1] => 21:30
)
[01.07] => Array
(
[0] => 20:30
)
)
bam! Thank you guys!
Upvotes: 1
Reputation: 4810
Create an array to store the values in.
$group = array();
foreach( $Show->Time as $Date ) {
$_date = new DateTime( $Date );
//if this is a new day, add it to the group
if( ! isset($group[$_date->format('m.d')]) )
$group[ $_date->format('m.d') ] = array();
//add the time to the array, organized by month.day
$group[ $_date->format('m.d') ][] = $_date->format('H:i');
}
//now you can iterate through the array to print everything out by day/time
foreach( $group as $month => $times ) {
echo $month . ': <br />';
foreach( $times as $time ) {
echo $time . '<br />';
}
}
Upvotes: 0
Reputation: 50787
Create an empty array outside of both of the foreach loops.
$grouped = array();
Then after you create your variables..
$day = date_format($Timecon, 'd.m.');
$hours = date_format($Timecon, 'H:i')....
Then push the date as a key and the hours as a value.
$grouped[$day] = $hours;
Then when you dump the array, you'll have an array that looks something like this:
Array(
[5.01] = Array(
String [5] = '17:30',
String [5] = '20:30'
),
[6.01] = Array(
String [5] = '17:30',
String [5] = '20:30'
),
);
And so forth.
Upvotes: 1