[英]Illegal offset type php function
我收到了此代码,由于非法的偏移量类型而遇到麻烦。
function getChildren($xmlSet){
global $output;
if(is_object($xmlSet)){
if(count($xmlSet->children()) > 0){
foreach($xmlSet->children() as $i => $child){
if($child->getName() === "label"){
$output[(string)$xmlSet->attributes()['id']] = getChildren($child);
} else {
if($child->getName() === "field" || $child->getName() === "fieldset"){
$output[$xmlSet->attributes()['id']] = getChildren($child);
}
}
}
你有一次
$output[(string)$xmlSet->attributes()['id']] = getChildren($child);
但是第二次
$output[$xmlSet->attributes()['id']] = getChildren($child);
第一次对字符串进行显式转换,但第二次隐式进行,因此发出警告。
某些对象类型(例如,特别是
SimpleXMLElement
将通过魔术方法__toString()
返回字符串表示形式进行打印/回显,但不能作为常规字符串使用。 尝试将它们用作数组键将产生非法的偏移类型错误,除非您通过(string)$obj
( ref )将它们强制转换为正确的字符串
问题是您正在尝试通过simpleXml对象访问数组元素。 在访问数组元素之前,必须将其强制转换为标量类型值。
所以改变这个:
$output[$xmlSet->attributes()['id']] = getChildren($child);
对此:
$output[(string)$xmlSet->attributes()['id']] = getChildren($child);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.