简体   繁体   English

PHP:SimpleXML和数组

[英]PHP: SimpleXML and Arrays

When I run this code: 当我运行此代码时:

foreach($xml->movie as $movie) { 
    if(isset($movie->photos)) { 
        foreach ($movie->photos as $photo) { 
            echo $photo." "; 
        } 
        echo "<hr/>"; 
    }
}

I get nice output of the actual data, eg a row looks like 我得到了很好的实际数据输出,例如一行看起来像

06397001.jpg 06397002.jpg 06397003.jpg 06397004.jpg 06397005.jpg 06397001.jpg 06397002.jpg 06397003.jpg 06397004.jpg 06397005.jpg

But when I throw it in an array, it includes all the SimpleXML wrapper tags and the jpgs are not at the root of the array. 但是,当我将其放入数组时,它包含所有SimpleXML包装器标签,而jpgs不在数组的根目录。

code: 码:

foreach($xml->movie as $movie) { 
    if(isset($movie->photos)) { 
        $photos = array(); 
            foreach ($movie->photos as $photo) { 
                $photos[] = $photo; 
            } 
    } else $photos = ""; 
    var_dump($photos); 
    echo "<hr />"; 
}

eg same row looks like 例如同一行看起来像

array(5) { 
    [0]=> object(SimpleXMLElement)#11 (1) { 
        [0]=> string(12) "06397001.jpg" 
    }
    [1]=> object(SimpleXMLElement)#12 (1) {
        [0]=> string(12) "06397002.jpg" 
    } 
    [2]=> object(SimpleXMLElement)#13 (1) {
        [0]=> string(12) "06397003.jpg"
    }
    [3]=> object(SimpleXMLElement)#14 (1) {
        [0]=> string(12) "06397004.jpg"
    }
    [4]=> object(SimpleXMLElement)#15 (1) {
        [0]=> string(12) "06397005.jpg"
    }
}

Why is this happening/how can I remove this so I just get an array of the photos at root level like when I echo it? 为什么会发生这种情况/如何删除它,以便像在回显时一样在根级别获得一系列照片?

SimpleXMLElement results look like simple objects and arrays, but do some magical things that normal objects and arrays can't do. SimpleXMLElement结果看起来像简单的对象和数组,但是做了一些普通对象和数组无法做到的神奇事情。 It's designed this way so that you can use less code. 它是通过这种方式设计的,因此您可以使用更少的代码。

You can probably solve your problem with something like: 您可能可以通过以下方式解决您的问题:

foreach($xml->movie as $movie) { 
  if(isset($movie->photos)) {
    $photos = array();
    foreach ($movie->photos as $photo) {
      $photos[] = "$photo"; 
      // or you could use, I believe: $photos[] = $photo[0] 
    }
  } 
  else $photos = "";
  var_dump($photos); echo "<hr />";
}

Why is this happening? 为什么会这样呢? Because a SimpleXML element when treated as a string will behave as if it's a string, with the value being that element's text contents. 因为当将SimpleXML元素视为字符串时,其行为就像是字符串一样,其值是该元素的文本内容。 In your first example, the use of echo was treating the element as a string so it just returned the string. 在您的第一个示例中,echo的使用将元素视为字符串,因此它只是返回了字符串。

I know this question is veeery old and it has already been answered, but since i had this issue today and i found relatively better and simplistic solution that does not involve tons of looping, I want to share it with you, guys. 我知道这个问题已经很老了,而且已经得到了回答,但是由于我今天遇到了这个问题,并且我发现了相对更好,更简单的解决方案,并且不涉及大量循环,所以我想与大家分享一下。

First off, having in mind your xml structure, and the fact that you don't need the movie's info, you dont need to loop through them, but to gather all photos, and this can be done via xpath only. 首先,考虑到您的xml结构以及您不需要电影信息的事实,您不需要遍历它们,而是收集所有照片,并且只能通过xpath来完成。

if you have multiple movies 如果你有多部电影

$photos = $xml->xpath('//movie//photos'); // double slash means get all elements with that tag name.

In other words get ALL photos from ALL movies. 换句话说,从所有电影中获取所有照片。 This code will give you an array of SimpleXMLElements which is array with key [0] and value of what the image's name is. 此代码将为您提供一个SimpleXMLElements数组,该数组具有键[0]和图像名称的值。 In order to get the image name without looping just to get it as a string, you can simply cast that element as a string and it will be outputted as a string, instead of SimpleXMLElement! 为了获得图像名称而不会仅仅将其作为字符串循环,可以简单地将该元素转换为字符串,然后将其输出为字符串,而不是SimpleXMLElement!

The only loop you might need now is: 您现在可能需要的唯一循环是:

foreach($photos as $photo) {
    dump( (string) $photo); // this will output the image name as a string e.g. "06397002.jpg"
}

Cheers, I hope it helps someone who needs it 干杯,我希望它能帮助需要它的人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM