simpleXMLElement attributes and foreach

自闭症网瘾萝莉.ら 提交于 2019-12-02 17:33:39

问题


If I run in the debugger, the program performs just 2 iterations correctly, and then do an infinite loop. If I exchange the line with foreach, it works correctly. Why?

test.xml:

<?xml version="1.0" encoding="utf-8"?>
<response result="0">
    <reports>
        <get count="2">
            <row a="first" b="second" comment="test" c=""/>
            <row a="first1" b="second2" comment="test2" c=""/>
        </get>
    </reports>
</response>

PHP:

$xml = simplexml_load_file('test.xml');
$rows = $xml->xpath('reports/get/row');
foreach($rows as $row){
        $item = [];
        $attr = $row->attributes();
        print_r($attr);
        $i = 0;
        foreach($attr as $key => $value){
        //foreach($row->attributes() as $key => $value){               
               $item[$key] = (string) $value;
               $i++;
        }
    $arr[] = $item;
}
return $arr;

来源:https://stackoverflow.com/questions/26533308/simplexmlelement-attributes-and-foreach

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!