简单的 PHP SimpleXML 问题

发布于 2024-09-27 19:20:25 字数 688 浏览 3 评论 0原文

我对此有严重的脑梗塞。

我的 XML 文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <fruits>
    <item>Apple</item>
    <item>Orange</item>
    <item>Banana</item>
  </fruits>
  <vegetables>
    <item>Lettuce</item>
    <item>Carrot</item>
  </vegetables>
</data>

我试图使用 SimpleXML 来检索包含“Apple、Orange、Banana”的数组。我使用的代码如下:

$xml=simplexml_load_file('food.xml');

foreach($xml as $fruits=>$item) {
  $foodlist[] = $item;
}

print_r($foodlist); // Should display list of fruits.

但是水果列表没有存储到数组中。我做错了什么?

非常感谢。

I've got a really bad brain block with this.

My XML file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <fruits>
    <item>Apple</item>
    <item>Orange</item>
    <item>Banana</item>
  </fruits>
  <vegetables>
    <item>Lettuce</item>
    <item>Carrot</item>
  </vegetables>
</data>

I am tyring to use SimpleXML to retrieve an array containing "Apple, Orange, Banana". The code I am using is as follows:

$xml=simplexml_load_file('food.xml');

foreach($xml as $fruits=>$item) {
  $foodlist[] = $item;
}

print_r($foodlist); // Should display list of fruits.

But the list of fruits is not being stored to the array. What am I doing wrong?

Much thanks.

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

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

发布评论

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

评论(4

蹲墙角沉默 2024-10-04 19:20:25

怎么样:

foreach($xml->fruits->item as $item) {
    //$item has to be cast to a string otherwise it will be a SimpleXML element
    $foodlist[] = (string) $item;
}
print_r($foodlist);

我认为这应该为您提供您正在寻找的内容,一个数组,其中包含作为水果节点子节点的每个项目节点的文本值。

How about this:

foreach($xml->fruits->item as $item) {
    //$item has to be cast to a string otherwise it will be a SimpleXML element
    $foodlist[] = (string) $item;
}
print_r($foodlist);

I think this should give you what you're looking for, an array containing the text value of each of the item nodes that are children of the fruits node.

じ违心 2024-10-04 19:20:25

我已经测试了你的代码。它对我来说效果很好。
还有一件事——可能是你描述错误,或者可能是你理解错误。 $foodlist 应包含 SimpleXML 元素对象数组(在您的情况下为“”和“”),而不是水果数组。如果您只想获取水果,您应该访问 $xml->fruits->item;

编辑:
如果你想构建一系列水果,请尝试以下操作:

$array = (array)$xml->fruits;
print_r($array['item']); // Should dipslay list of fruits

//or this
foreach ($xml->fruits->item as $fruit){
  $array2[] = (string) $fruit; //typecast to string, because $fruit is xml element object.
}
print_r($array2); // Should dipslay list of fruits

I've tested your code. It works fine for me.
Another thing - it might be that you described it wrong, or might be that you understand this wrong. $foodlist should contain array of SimpleXML element objects (in your case "<fruits>" and "<vegetables>"), not array of fruits. If you want get only fruits you should access $xml->fruits->item;.

Edit:
if you want to build an array of fruits try this:

$array = (array)$xml->fruits;
print_r($array['item']); // Should dipslay list of fruits

//or this
foreach ($xml->fruits->item as $fruit){
  $array2[] = (string) $fruit; //typecast to string, because $fruit is xml element object.
}
print_r($array2); // Should dipslay list of fruits
美煞众生 2024-10-04 19:20:25

尝试 foreach( $xml['data'] as $fruits=>$item )

---- 编辑 ----

foreach( $xml as $fruits => $item ) {
    if( $fruits == "fruits" )
        $foodlist[] = $item;
}

Try foreach( $xml['data'] as $fruits=>$item )

---- edit ----

foreach( $xml as $fruits => $item ) {
    if( $fruits == "fruits" )
        $foodlist[] = $item;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文