arrays – php – How do I fix this illegal offset type error

arrays – php – How do I fix this illegal offset type error

Illegal offset type errors occur when you attempt to access an array index using an object or an array as the index key.

Example:

$x = new stdClass();
$arr = array();
echo $arr[$x];
//illegal offset type

Your $xml array contains an object or array at $xml->entry[$i]->source for some value of $i, and when you try to use that as an index key for $s, you get that warning. Youll have to make sure $xml contains what you want it to and that youre accessing it correctly.

Use trim($source) before $s[$source].

arrays – php – How do I fix this illegal offset type error

check $xml->entry[$i] exists and is an object
before trying to get a property of it

 if(isset($xml->entry[$i]) && is_object($xml->entry[$i])){
   $source = $xml->entry[$i]->source;          
   $s[$source] += 1;
 }

or $source might not be a legal array offset but an array, object, resource or possibly null

Leave a Reply

Your email address will not be published. Required fields are marked *