我正在尝试阅读下面列出的XML
输出:
SimpleXMLElement Object
(
[merchant] => SimpleXMLElement Object
(
[merchant-id] => SimpleXMLElement Object
(
)
[merchant-name] => Snip-Its
[merchant-category] => Hair
[merchant-website] => www.snipits.com
)
[store] => SimpleXMLElement Object
(
[store-id] => 173DFBB7-67F6-DE11-AD5B-0026B953348A
[store-name] => Heb
[store-number] => 0109
)
[coupon] => SimpleXMLElement Object
(
[coupon-id] => 385748
[coupon-address] => 14096 Memorial Drive
[coupon-city] => Houston
[coupon-state] => TX
)
)
使用PHP
脚本,如下所示,但我得到的值为'0':
foreach( $data->coupons as $mycoupon ) {
$address = $mycoupon->coupon->coupon-address;
//print_r($mycoupon);
}
所以,请帮助我理解如何阅读SimpleXMLElement
对象的值并将它们分配给局部变量。
答案 0 :(得分:1)
某些字符不允许用于PHP的命名约定,例如连字符。要处理这个问题,只需将元素名称括在括号中作为字符串:
$address = $mycoupon->coupon->{'coupon-address'};
试一试。