【问题标题】:How to access nested XML如何访问嵌套的 XML
【发布时间】:2014-06-29 21:34:43
【问题描述】:

您好,我正在使用 XML 提要,我需要访问 XML 循环中的循环。

基本上我拥有的xml是这样的:

<properties>
    <property>
        <images>
            <image modified="2012-04-03 19:20:16">http://image.url/</image>
            <image modified="2012-04-03 19:20:16">http://image.url/</image>
            <image modified="2012-04-03 19:20:16">http://image.url/</image>
            <image modified="2012-04-03 19:20:16">http://image.url/</image>
            <image modified="2012-04-03 19:20:16">http://image.url/</image>
            <image modified="2012-04-03 19:20:16">http://image.url/</image>
        </images>
    </property>
</properties>

我有这个循环:

foreach($xml->property as $property) {
    foreach($property->images->image as $key => $value) {
        print_r($value);
    }
}

但是 $value 正在返回 [@attributes] => Array ([modified] => 2013-10-03 11:53:47

我希望返回 http://image.url/

有什么想法吗?

谢谢,

汤姆

【问题讨论】:

    标签: php xml foreach


    【解决方案1】:

    您需要将值转换为字符串,例如:

    foreach($xml->property as $property) {
        foreach($property->images->image as $img) {
            $value = (string) $img;
            echo $value;
        }
    }
    

    【讨论】:

    • 当我回显它时效果很好,但由于某种原因,当我将它放入 MySQL 查询以将其设置到数据库中时,它放入 0。你知道为什么吗?
    • 知道了!谢谢,这是 Wordpress 的问题。无论如何,谢谢!
    猜你喜欢
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    相关资源
    最近更新 更多