Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从SimpleXMLElement对象检索值_Php_Object_Simplexml - Fatal编程技术网

Php 如何从SimpleXMLElement对象检索值

Php 如何从SimpleXMLElement对象检索值,php,object,simplexml,Php,Object,Simplexml,调用prestashop Web服务后,我收到如下响应: SimpleXMLElement Object ( [order] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array (

调用prestashop Web服务后,我收到如下响应:

SimpleXMLElement Object
(
    [order] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 1
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 2
                        )

                )

        )

)
我尝试通过如下所示的循环来检索内容:

foreach($resources as $resource){
    echo '<pre>';
    print_r($resource['id']);
    echo '</pre>';
}
如何检索这些对象的值1和2?谢谢我讨厌simplexml

$value)
{
$attrs=$value->attributes();
foreach($attrs as$attr\u k=>$attr\u v)
echo$attr_k.:“$attr_v.\n”;
}

如果可以,请将其替换为DOMGreat文档。非常感谢你所做的一切。
SimpleXMLElement Object
(
    [0] => 1
)
SimpleXMLElement Object
(
    [0] => 2
)
<?php
$xml = file_get_contents('xml.xml');
$xml = new SimpleXMLElement($xml);

foreach($xml as $key => $value)
{
        $attrs = $value->attributes();
        foreach($attrs as $attr_k => $attr_v)
                echo $attr_k.": ".$attr_v."\n";
}