解析错误:语法错误,意外的 T_OBJECT_OPERATOR [英] Parse error: syntax error, unexpected T_OBJECT_OPERATOR
问题描述
尝试将 myXml.xml 中的一些数据添加到字符串时,出现以下错误:解析错误:语法错误,意外的 T_OBJECT_OPERATOR.
I'm getting the following error when trying to add some data from myXml.xml to a string: Parse error: syntax error, unexpected T_OBJECT_OPERATOR.
$xmlstr = file_get_contents('myXml.xml');
$xml = new SimpleXMLElement($xmlstr);
foreach($xml->order as $order){
$replace = array();
$firstName = (string) $order->billing-address->first-name;
$lastName = (string) $order->billing-address->last-name;
}
我无法直接提供我的 XML,因为它包含敏感数据.
I can't provide my XML directly as it contains sensitive data.
谢谢,山姆
推荐答案
-
符号表示减法.要在属性名称中使用它,您必须使用以下语法:
The -
sign means subtraction. To use it in property names, you must use this syntax:
$firstName = (string) $order->{"billing-address"}->{"first-name"};
$lastName = (string) $order->{"billing-address"}->{"last-name"};
一般来说,最好使用 firstName
、billingAddress
等作为属性名称来避免这种情况.请参阅CamelCase.但是,在这种情况下,您可能无法控制 XML 输入.
In general, it's probably better to use firstName
, billingAddress
, etc. as property names to avoid this. See CamelCase. In this case, however, you may have no control over the the XML input.
这篇关于解析错误:语法错误,意外的 T_OBJECT_OPERATOR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!