Search code examples
phpxmlxml-parsingsimplexml

Unable to iterate of SimpleXMLElement objects and insert into array


I have the following XML:

<channels>
  <channel>
    <name>Channel 1</name>
    <tid>15179</tid>
  </channel>
  <channel>
    <name>Channel 2</name>
    <tid>15177</tid>
  </channel>
  <channel>
    <name>Channel 3</name>
    <tid>15176</tid>
  </channel>
  <channel>
    <name>Channel 4</name>
    <tid>15407</tid>
  </channel>
  <channel>
    <name>Channel 5</name>
    <tid>15175</tid>
  </channel>
  <channel>
    <name>Channel 6</name>
    <tid>15178</tid>
  </channel>
</channels>

and using SimpleXMLElement, I get this object:

SimpleXMLElement Object
(
    [channel] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [name] => Channel 1
                    [tid] => 15179
                )

            [1] => SimpleXMLElement Object
                (
                    [name] => Channel 2
                    [tid] => 15177
                )

            [2] => SimpleXMLElement Object
                (
                    [name] => Channel 3
                    [tid] => 15176
                )

            [3] => SimpleXMLElement Object
                (
                    [name] => Channel 4
                    [tid] => 15407
                )

            [4] => SimpleXMLElement Object
                (
                    [name] => Channel 5
                    [tid] => 15175
                )

            [5] => SimpleXMLElement Object
                (
                    [name] => Channel 6
                    [tid] => 15178
                )

        )

)

I am trying to simply iterate this object to put the channels into an array, but no matter how I try to iterate, all I get is SimpleXMLElement objects, and apparently it is not allowed to use them as array keys. For instance, thise code

$channel_data = new SimpleXMLElement($channel_xml->data);
foreach ($channel_data->children() as $nb_channel) {
  $channel_name = $nb_channel->name;
  $channel_tid = $nb_channel->tid;
  $target_channels[$channel_tid[0]] = $channel_name[0];
}

$channel_name and $channel_tid come out as SimpleXMLElements, too. If I try

foreach ($channel_data->channel as $nb_channel) {
  $channel_name = $nb_channel->name;
  $channel_tid = $nb_channel->tid;
  $target_channels[$channel_tid] = $channel_name;
}

$channel->name and $channel->tid are simpleXMLElement objects as well.

SimpleXMLElement Object
(
    [name] => Channel 1
    [tid] => 15179
)

But attempting to access them as

   $nb_channel->name;

just gives me another object:

SimpleXMLElement Object
(
    [0] => Channel 1
)

What is the correct way to iterate over the XML to get my data and put it into an array?

Thanks.


Solution

  • And as usual, the "post a question and then figure out the answer" principle is in effect.

    The trick was casting the values to string with __toString():

    $channel_data = new SimpleXMLElement($channel_xml->data);
    foreach ($channel_data->channel as $nb_channel) {
      $channel_name = $nb_channel->name->__toString();
      $channel_tid = $nb_channel->tid->__toString();
      $target_channels[$channel_tid] = $channel_name;
    }
    

    Once I did that, my variables has the correct data, and I was able to fill the $target_channels array.