dougaimian1143 2016-01-30 01:54
浏览 72
已采纳

无法迭代SimpleXMLElement对象并插入到数组中

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.

  • 写回答

1条回答 默认 最新

  • doufei1893 2016-01-30 02:39
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成