I got this code and I am having trouble because of illegal offset type.
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);
}
}
}
You have this once
$output[(string)$xmlSet->attributes()['id']] = getChildren($child);
but the second time you have
$output[$xmlSet->attributes()['id']] = getChildren($child);
The first time did an explicit cast to a string, but the second did so implicitly, hence the warning.
Some object types, notably
SimpleXMLElement
for example, will return a string representation to print/echo via the magic method__toString()
, but cannot stand in as regular strings. Attempts to use them as array keys will yield the illegal offset type error unless you cast them to proper strings via(string)$obj
( ref )
The issue is that you're trying to access an array element via a simpleXml object. Before you can access the array element you must cast it to a scalar type value.
So change this:
$output[$xmlSet->attributes()['id']] = getChildren($child);
To this:
$output[(string)$xmlSet->attributes()['id']] = getChildren($child);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.