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 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?