119

I'm getting

illegal offset type

error for every iteration of this code. Here's the code :

$s = array();
for ($i = 0; $i < 20; $i++)
{
    $source = $xml->entry[$i]->source;
    $s[$source] += 1;    
}

print_r($s);
1
  • 16
    Warning: almost all answers (except zombat's) assume that $source is an instance of SimpleXML and provide information that only applies to that specific situation. While that was eventually the case, the question didn't state it and whoever comes here for reference should take this into account. Commented Jan 12, 2017 at 11:01

6 Answers 6

195

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. You'll have to make sure $xml contains what you want it to and that you're accessing it correctly.

Sign up to request clarification or add additional context in comments.

6 Comments

the source contains html, does that class as an object?
Did you create the $xml variable using some kind of XML parser? simple_xml or DOMDocument? In that case, then it's likely that the source node is actually some kind of dom element object.
i'm using simplexml_load_string. does that help?
Your HTML might have been parsed as XML, and likely all your tags became nodes. For example, if you had an HTML snippet of "<div>Hi</div>" as your source property, then you've probably got something like $xml->entry[$i]->source->div. If you want to parse HTML into a DOM structure, DomDocument has a loadHTML() function that handles HTML much better than SimpleXML. Check out php.net/manual/en/domdocument.loadhtml.php
thanks for that. I've used str_replace to strip the html and it works.
|
27

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

3 Comments

Trimming an object is just a non-obvious way to cast it to string (the straightforward one would be (string)$source) and results depends entirely on its __toString() implementation. It works if you have a SimpleXML object (something apparently assumed by everybody but never really stated in the question).
If this is issue is related with __toString() implementation call trim() is not clean solution. It is confusing.
you can also try a gettype($source) which would probably return object not string or number as expected.
5

Check whether $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;
 }

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

Comments

0

I had a similar problem. As I got a Character from my XML child I had to convert it first to a String (or Integer, if you expect one). The following shows how I solved the problem.

foreach($xml->children() as $newInstr){
        $iInstrument = new Instrument($newInstr['id'],$newInstr->Naam,$newInstr->Key);
        $arrInstruments->offsetSet((String)$iInstrument->getID(), $iInstrument);
    }

Comments

0

I got the illegal offset type error when I was defining an array incorrectly, like so:

const ENGLISH_LANGUAGE = 1;

const LANGUAGE_LANGCODES = [
  [ENGLISH_LANGUAGE] => 'en'
];

The correct definition doesn't include [] around the key:

const ENGLISH_LANGUAGE = 1;

const LANGUAGE_LANGCODES = [
  ENGLISH_LANGUAGE => 'en'
];

Comments

-1

There are probably less than 20 entries in your xml.

change the code to this

for ($i=0;$i< sizeof($xml->entry); $i++)
...

1 Comment

An undefined integer index doesn't generate an "Illegal offset" warning, you'd get an "Undefined index" E_NOTICE instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.