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.

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

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入