0

我真的很困惑为什么会发生这种情况(相当多的时间,比我想承认的要多)。基本上,当它到达 Date 时,它​​不会将日期添加到数组中,而 Time 绝对没问题。

XML 代码示例

<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 代码示例

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
(
    [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
        )

问题

所以基本上问题是日期(日、月、工作日)应该输入到 [leave][date] ..(示例)[leave][date][day]

即使没有 IF 语句,并且仅将其输入 [leave] 数组也不起作用,例如

$x[$i]['leave'][$attr->nodeName] = $attr->nodeValue;

所以我基本上不确定并且不明白为什么它会显示在第一个数组中(例如按照示例在离开旁边)

$x[$i][$attr->nodeName] = $attr->nodeValue;

对此的任何建议或帮助将不胜感激。

4

1 回答 1

1
foreach ($node->childNodes as $child) {
    ...
    $x[$i]['leave'] = array();
    $x[$i]['leave']['date'] = array();
    $x[$i]['leave']['time'] = array();
    ...
}

此循环运行两次, foritdDateitdTime,正如您在第二次迭代中看到的那样,它正在用空的数组覆盖先前填充的数组。

您应该在覆盖它们之前检查这些数组是否不存在,或者在遍历itdDateTime子节点之前更好地移动它们的定义:

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;
            }
        }
    }
}
于 2012-10-28T12:23:25.533 回答