0

I'm creating an array using an xml file. I've done this before so I don't know why it's a problem, but when I use one of the variables as an array to assign another variable to the array, I get "Warning: Illegal offset type".

Here's what I'm doing:

$stateData = array(); 
$stateData["99"]="All States";
    $stateXmlFile = simplexml_load_file("../../resources/StateCodes.xml");
    foreach($stateXmlFile->state as $stateskey){    
        $offset = $stateskey->number;
        $stateData[$offset] = $stateskey->alpha;
    }

The $stateskey->number from xml will be a two digit number. I've done things like this before and never had any issues. If somebody could please tell me what I'm doing wrong and how to work around this, I'd be greatly appreciative.

Thanks.

4
  • 5
    Are you sure that $stateskey->number is an integer (and not a SimpleXML object or the like)? Do a var_dump of it, please. Commented Oct 30, 2013 at 20:19
  • and share the XML, pls Commented Oct 30, 2013 at 21:00
  • I'd say this is the common "need to cast to string" simplexml mistake. It happens often in the beginning, see the linked duplicate, despite not accepted that is exactly the solution even with a similar context. Commented Nov 2, 2013 at 20:44
  • In your concrete case you probably want to make use of intval and trim: eval.in/59898 - and another duplicate here: PHP XML: Illegal Offset, but it is in array? (Sep 2011) Commented Nov 2, 2013 at 20:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.