简体   繁体   English

读取 XML 并存储到 PHP 中的 arrays

[英]Reading XML and storing to arrays in PHP

I have this:我有这个:

$xml = simplexml_load_file('test.xml');
print"<pre>";
print_r($xml);

It printout this:它打印出这个:

SimpleXMLElement Object
(
    [b] => SimpleXMLElement Object
        (
            [c] => SimpleXMLElement Object
                (
                    [d] => 543
                )

        )

)

but when I type echo $xml["b"]["c"]["d"];但是当我输入echo $xml["b"]["c"]["d"]; nothing happens什么都没发生

the print_r is kind of misleading, print_r有点误导,
actually the $xml is series/array of SimpleXmlElement objects实际上$xml是 SimpleXmlElement 对象的系列/数组

so所以

echo (int)$xml->b->c->d;  --- type casting is required

here is some reference you should take a look first这里有一些参考,你应该先看看

Additional to type casting ,除了类型转换
because everything node inside the xml object is either string or int因为 xml object 中的所有节点都是stringint

PHP will auto convert for numeric string to integer, PHP 将自动将数字字符串转换为 integer,
however, is clearer if you provide the type hinting但是,如果您提供类型提示,则更清楚

var_dump($xml); --- you should see more information on the data type

Try echo $xml->b->c->d;试试echo $xml->b->c->d;

$xml is not array they are objects. $xml 不是数组,它们是对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM