可能重複:
SimpleXML Reading node with a hyphenated name解析xml文件時如何解決錯誤「使用未定義的常量id - 假定'id'」?
我解析包含這樣的領域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>
使用$xml = simplexml_load_file($file);
第一,試圖讓foreach循環I值後獲取錯誤「使用未定義的常量id - 假定'id'」字段包含'id'作爲它們的一部分,如'type-id'或'categ ORY-ID」
foreach($xml->offers->offer as $offer) {
echo $offer->type; // WORKS JUST FINE
echo $offer->type-id; //THIS GIVE ME ERROR
}
我試圖設置ini_set('error_reporting', E_ALL & ~E_NOTICE);
但它與後場‘身份證’歸零,而不是價值。
要訪問裏面有'-'的值,你需要把它們作爲字符串進入括號:'echo $ offer - > {'type-id'};' – Peon
是的,我發現它'echo $ offer - > {'type-id'};'工作。 – electroid
更多信息http://stackoverflow.com/questions/3214153/parse-error-syntax-error-unexpected-t-object-operator – electroid