Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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 SimpleXml访问属性_Php_Foreach_Simplexml - Fatal编程技术网

Php SimpleXml访问属性

Php SimpleXml访问属性,php,foreach,simplexml,Php,Foreach,Simplexml,我正在尝试使用SimpleXML访问属性 以下是我的XML格式的一个示例: <xml> <config> <anothernode></anothernode> <anothernode></anothernode> <anothernode></anothernode> <anothernode></anoth

我正在尝试使用SimpleXML访问属性

以下是我的XML格式的一个示例:

<xml>
    <config>
        <anothernode></anothernode>
        <anothernode></anothernode>
        <anothernode></anothernode>
        <anothernode></anothernode>
        <anothernode></anothernode>
        <popup name="rory" type="y" />
        <popup name="joe" type="z" />
        <popup name="pat" type="u" />
        <popup name="mary" type="v" />
    </config>
</xml>

这呼应出第一个弹出名称,即“Rory”:

echo“导出:”$xml->config->popup['name']。“
”;
这呼应出第一种类型,即“y”

echo“导出:”$xml->config->popup['type']。“
”;
我试着通过所有的循环得到所有的值,所以我会有这样的东西

罗里 乔-z 帕特-u 玛丽五世

我拙劣的尝试如下所示,但我尝试过的其他变体也没有成功,我刚刚开始学习simpleXML,还没有完全了解它

foreach($xml->config->popup['name'] as $node => $key){
    //
    echo $node . " - ". $xml->config->popup['$key']['type'] . "<br />";
    }
foreach($xml->config->popup['name']作为$node=>$key){
//
echo$node。“-”$xml->config->popup['$key']['type'].“
”; }

现在,如果xml的结构更好,并且popup元素位于主popups元素下,我可以使用children()选项循环浏览每个弹出窗口并访问它,但是我正在处理的xml中的这种不寻常的格式在internet上似乎没有任何可以告诉我如何在foreach循环中访问它们的示例。有什么想法吗?

您需要在
弹出窗口上循环,例如

foreach ($xml->config->popup as $popup) {
    echo $popup['name'], ' - ', $popup['type'], '<br>';
}
foreach($xml->config->popup as$popup){
echo$popup['name']、'-'、$popup['type']、“
”; }
演示-(我还修复了您的XML)

foreach($xml->config->popup['name'] as $node => $key){
    //
    echo $node . " - ". $xml->config->popup['$key']['type'] . "<br />";
    }
foreach ($xml->config->popup as $popup) {
    echo $popup['name'], ' - ', $popup['type'], '<br>';
}