使用'simplexml'php遍历xml文件图像

问题描述 投票:0回答:1

Google搜索了数小时,没有喜悦。

iam使用简单的xml并对我的xml文件进行各种查询。我可以显示一张图像(1张图像)精细。

我无法获取foreach循环来显示所有图像。好吧,我可以,但是我用了[i ++],它显示了所有图像,但不会停止循环!

下面显示2张图像。首先是显示1张图片的基本显示和我的理解。第二张图片是我希望所有循环发生的地方。

$reference  = $_POST['varname'];
$xml =  simplexml_load_file('save.xml') or die("can not find file");
$result = $xml->xpath("//property[property_reference='$reference']");
foreach ( $result as $elements){
<img class="card-img-top" height='240px' width='340px' src=" <?php echo 
$elements->pictures->picture[0]->filename  ; ?> " alt="Card image cap"    > 
} ;  
<br>
<?php foreach( $elements as $image) { ?>
<img class="card-img-top" height='240px' width='340px' src=" <?php echo 
$elements->pictures->picture[All of the images]->filename ; ?> " alt="Card image cap"> }?> 
php xpath foreach element simplexml
1个回答
0
投票

似乎您需要在结构中更深入地循环,因为您拥有一个数组中的一个数组。

解决方案1:深入循环

<?php

$reference  = $_POST['varname'];
$xml = simplexml_load_file('save.xml') or die("can not find file");
$result = $xml->xpath("//property[property_reference='$reference']");
foreach ($result as $elements) {
    foreach ($elements as $pictures) {
        foreach ($pictures as $picture) { ?>
            <img class="card-img-top" height='240px' width='340px' src="<?php echo $picture->filename?> " alt="Card image cap"> 
        <?php }
    }
}

解决方案2:使用更深的XPath:

<?php

$reference  = $_POST['varname'];
$xml = simplexml_load_file('save.xml') or die("can not find file");
$pictures = $xml->xpath("//property[property_reference='$reference']/pictures");
foreach ($pictures as $picture) { ?>
    <img class="card-img-top" height='240px' width='340px' src="<?php echo $picture->filename?> " alt="Card image cap">
<?php }
© www.soinside.com 2019 - 2024. All rights reserved.