-2

我在下面有以下 XML 结构,但我不确定如何直接访问该 URL,以便可以将它用作src我的图像标记中的一个。

PHP结构:

$images = $url->property->photos
<img src="'.$images.'" width="233" height="154" /> - Testing URL  

通用 XML 结构:

<photos>
 <photo>
  Information
 </photo>
</photos>

XML结构:

["photos"]=>
    object(SimpleXMLElement)#118 (2) {
      ["@attributes"]=>
      array(1) {
        ["count"]=>
        string(2) "43"
      }
      ["photo"]=>
      array(43) {
        [0]=>
        string(97) "http://www.domain.co.nz/images/6f8bc4855bface1ad2c35635625fdbb4c3a11537.jpeg"
        [1]=>
        string(97) "http://www.domain.co.nz/images/fcb36e4a3465b934490d44e6bb8e1a8192cb466d.jpeg"
        [2]=>
        string(97) "http://www.domain.co.nz/images//c1e2c792832a08f881bb24756137a63a19c7070d.jpeg"
        [3]=>
        string(97) "http://www.domain.co.nz/images/b6d9a3c52c7fba81553edb9101c80d273c16ee3c.jpeg"
4

2 回答 2

1
$xmlstr = <<<XML
<properties count="1">
        <property approved="yesterday">
                <address>123 Main Street</address>
                <photos count="2">
                        <photo>1.jpg</photo>
                        <photo>2.jpg</photo>
                </photos>
        </property>
</properties>
XML;
$properties = new SimpleXMLElement($xmlstr);//this could have been done using simplexml_load_file($url); for instance
//the important part is that you have a SimpleXML object named $properties
foreach($properties->property as $property){
        echo $property['approved']."<br>\n";
        echo $property->address."<br>\n";
        foreach($property->photos->photo as $photo){
                echo $photo."<br>\n";
        }
}

我假设您可能有多个属性,并且您想列出每个属性。如果您只有一个,或者只想列出第一个,您可以安全地删除第一个 foreach 循环:

echo $properties->property[0]['approved']."<br>\n";
echo $properties->property[0]->address."<br>\n";
foreach($properties->property[0]->photos->photo as $photo){
    echo $photo."<br>\n";
}
于 2012-10-30T22:05:48.987 回答
0

如果你 var_dump'd 喜欢这样:

var_dump($images);
//output:
object(SimpleXMLElement)#117 (2) {...

您可以像这样循环访问这些照片 URL:

foreach($images->photo as $image){
    echo '<img src="'.$image.'"/>;
}
于 2012-10-30T20:57:46.327 回答