如果我在调试器中运行,程序只能正确执行2次迭代,然后执行无限循环。 如果我用foreach交换该行,它可以正常工作。 为什么呢?
的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;