Reputation: 1612
I am really confused with why this is happening have spent (quite a lot of time, more than I would like to admit). Basically when it gets to Date it does not add the date to the array whereas Time is absolutely fine.
XML Code Example
<xml>
<Route id="1">
<itdDateTime>
<itdDate day="28" month="10" year="2012" weekday="-1"/>
<itdTime hour="12" minute="53"/>
</itdDateTime>
</Route>
<Route id="2">
<itdDateTime>
<itdDate day="3" month="12" year="2012" weekday="-1"/>
<itdTime hour="8" minute="14"/>
</itdDateTime>
</Route>
<Route id="3">
<itdDateTime>
<itdDate day="3" month="12" year="2012" weekday="-1"/>
<itdTime hour="9" minute="16"/>
</itdDateTime>
</Route>
PHP Code Example
foreach($route->childNodes as $node) {
if ($node->nodeName == 'itdDateTime') {
foreach ($node->childNodes as $child) {
$dateAttr = $child->attributes;
$x[$i]['leave'] = array();
$x[$i]['leave']['date'] = array();
$x[$i]['leave']['time'] = array();
foreach ($dateAttr as $index=>$attr) {
$x[$i][$attr->nodeName] = $attr->nodeValue;
if ($index == 'minute' || $index == 'hour') {
$x[$i]['leave']['time'][$attr->nodeName] = $attr->nodeValue;
} else {
$x[$i]['leave']['date'][$attr->nodeName] = $attr->nodeValue;
}
}
}
}
}
Array example
Array
(
[0] => Array
(
[active] => 1
[delete] => 0
[changes] => 2
[distance] => 966
[alternative] => 0
[publicDuration] => 01:19
[individualDuration] => 00:22
[print] => 0
[routeIndex] => 1
[hasFrequency] => 1
[routeTripIndex] => 1
[cTime] => 20121028113640847
[searchMode] => 1
[vehicleTime] => 53
[method] => itp
[selected] => 1
[leave] => Array
(
[date] => Array
(
)
[time] => Array
(
[hour] => 12
[minute] => 53
)
)
[day] => 28
[month] => 10
[year] => 2012
[weekday] => 1
[hour] => 12
[minute] => 53
)
Problem
So basically the issue is that the date (day, month, weekday) should be entered into [leave][date] .. (example) [leave][date][day]
Even having no IF Statement, and entering it into [leave] array only doesnt work for example
$x[$i]['leave'][$attr->nodeName] = $attr->nodeValue;
So Im basically unsure and dont understand why it will show in the first array (such as next to Leave as per the example)
$x[$i][$attr->nodeName] = $attr->nodeValue;
Any advice or help on this would be greatly appreciated.
Upvotes: 0
Views: 136
Reputation: 29462
foreach ($node->childNodes as $child) {
...
$x[$i]['leave'] = array();
$x[$i]['leave']['date'] = array();
$x[$i]['leave']['time'] = array();
...
}
This loop runs twice, for itdDate
and itdTime
, and as you can see on second iteration it is overwriting previously filled array with empty one.
You should check if these arrays does not exist before overwriting them, or better move their definition before looping through itdDateTime
child nodes:
if ($node->nodeName == 'itdDateTime') {
$x[$i]['leave'] = array();
$x[$i]['leave']['date'] = array();
$x[$i]['leave']['time'] = array();
foreach ($node->childNodes as $child) {
$dateAttr = $child->attributes;
foreach ($dateAttr as $index=>$attr) {
$x[$i][$attr->nodeName] = $attr->nodeValue;
if ($index == 'minute' || $index == 'hour') {
$x[$i]['leave']['time'][$attr->nodeName] = $attr->nodeValue;
} else {
$x[$i]['leave']['date'][$attr->nodeName] = $attr->nodeValue;
}
}
}
}
Upvotes: 1