我解析包含这样的字段的xml文件:
<offers>
<offer>
<type>Vehicle</type>
<type-id>2</type-id>
<category>Car</category>
<category-id>3</category-id>
...
</offer>
<offer>
<type>Vehicle</type>
<type-id>2</type-id>
<category>Car</category>
<category-id>3</category-id>
...
</offer>
...
</offers>
Run Code Online (Sandbox Code Playgroud)
$xml = simplexml_load_file($file);首先使用,然后尝试在foreach循环中获取值后,出现错误“使用未定义的常数id-假定的'id'”,其中字段包含“ id”作为它们的一部分,例如“ type-id”或“ category-id”
foreach($xml->offers->offer as $offer) {
echo $offer->type; // WORKS JUST FINE
echo $offer->type-id; //THIS GIVE ME ERROR
}
Run Code Online (Sandbox Code Playgroud)
我尝试设置,ini_set('error_reporting', E_ALL & ~E_NOTICE);但在它后面带有“ id”的字段返回零而不是值。