添加到关联数组

发布于 2024-11-19 10:19:40 字数 1297 浏览 0 评论 0原文

我有一些如下所示的 xml:

<?xml version="1.0"?>
    <data>
<items>
    <item><timestamp>2011-07-11T09:01:42Z</timestamp><title><![CDATA[ some data here ]]></title><link>http://twitter.com/aurl</link></item>
    <item><timestamp>2011-05-11T09:01:42Z</timestamp><title><![CDATA[ some data here ]]></title><link>http://twitter.com/aurlhere</link></item>
</items>

我试图循环它并将其添加到一个数组中:

 foreach($xml->items->item as $e) {
              $feedData['timestamp'] = $e->timestamp;
            $feedData['title'] = $e->title;
            $feedData['link'] = $e->link;
            $feedData['type'] = $e->type;
        }

    print_r($feedData);

xml 在那里,如果我在 foreach 中添加 echo,我会得到 10 个响应(其中是我有多少个项目),但是当我打印出数组时我得到的只是一行(这是 xml 文档中的最后一个条目)。

Array ( [timestamp] => SimpleXMLElement Object ( [0] => 2011-07-08T08:05:19Z ) [title] => SimpleXMLElement Object ( ) [link] => SimpleXMLElement Object ( [0] => http://twitter.com.aurl ) [type] => SimpleXMLElement Object ( ) ) 

我怀疑我在每个循环上覆盖了数组。但为什么 ?它应该添加到数组中。抱歉,我的 php 不太好......因此“怀疑”是一个有点愚蠢的问题。

I have some xml that looks like this:

<?xml version="1.0"?>
    <data>
<items>
    <item><timestamp>2011-07-11T09:01:42Z</timestamp><title><![CDATA[ some data here ]]></title><link>http://twitter.com/aurl</link></item>
    <item><timestamp>2011-05-11T09:01:42Z</timestamp><title><![CDATA[ some data here ]]></title><link>http://twitter.com/aurlhere</link></item>
</items>

and I'm trying to loop over it and add it to an array:

 foreach($xml->items->item as $e) {
              $feedData['timestamp'] = $e->timestamp;
            $feedData['title'] = $e->title;
            $feedData['link'] = $e->link;
            $feedData['type'] = $e->type;
        }

    print_r($feedData);

The xml is there and if I put an echo in the foreach I get back 10 responses (whcih are how many items I have) but when I print the array out all I get is one line (which is the last entry in the xml doc).

Array ( [timestamp] => SimpleXMLElement Object ( [0] => 2011-07-08T08:05:19Z ) [title] => SimpleXMLElement Object ( ) [link] => SimpleXMLElement Object ( [0] => http://twitter.com.aurl ) [type] => SimpleXMLElement Object ( ) ) 

I suspoect I'm overwriting the array on each loop. But why ? It should be adding to the array. Sorry my php isn't great...hence what Is suspect is a bit of a stupid question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-11-26 10:19:40
 $i = 0;
 foreach($xml->items->item as $e) { 
   $feedData[$i]['timestamp'] = $e->timestamp;
   $feedData[$i]['title'] = $e->title;
   $feedData[$i]['link'] = $e->link;
   $feedData[$i]['type'] = $e->type;
   $i++;
 }

print_r($feedData);
 $i = 0;
 foreach($xml->items->item as $e) { 
   $feedData[$i]['timestamp'] = $e->timestamp;
   $feedData[$i]['title'] = $e->title;
   $feedData[$i]['link'] = $e->link;
   $feedData[$i]['type'] = $e->type;
   $i++;
 }

print_r($feedData);
浅唱ヾ落雨殇 2024-11-26 10:19:40

嗯,只有一个 $feedData 数组,并且您总是为某些特定键设置值,例如 $feedData['timestamp']。您认为这应该如何添加到数组中?键是唯一的,相同的键不能出现两次,所以是的,您只是覆盖了值。

看来你想要一个数组的数组:

$feedData = array();

foreach($xml->items->item as $e) {
    $feedData[] = array(
        'timestamp' => $e->timestamp;
        'title' => $e->title;
        'link' => $e->link;
        'type' => $e->type;
    );
}

看看数组手册 [docs] 了解有关数组的更多信息。

Well, there is only one $feedData array and you are always setting the value for some specific keys, e.g. $feedData['timestamp']. How do you think this should add to the array? Keys are unique, the same key cannot appear twice, so yes, you are just overwriting the values.

It seems you want an array of arrays:

$feedData = array();

foreach($xml->items->item as $e) {
    $feedData[] = array(
        'timestamp' => $e->timestamp;
        'title' => $e->title;
        'link' => $e->link;
        'type' => $e->type;
    );
}

Have a look at the array manual [docs] for more information about arrays.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文