PHP Echo foreach来自XML

时间:2013-10-14 17:22:51

标签: php foreach echo

我正在阅读xml文件:

<imageref>image1.jpg|image2.jpg|image3.jpg|image4.jpg</imageref>
<hotelname>villa test</hotelname>


foreach ($filtered as $hotel) {     
    $xmlhotels[] = array(        
        'image'=>(string)$hotel->imageref,
        'villaname'=>(string)$hotel->hotelname
    );
}

当我回应别名的foreach值时,我明白了。

foreach ($myhotels as $villa) {    
echo"",$villa['villaname'],""; }

如何从xml文件中回显第一个图像(image1.jpg)。

1 个答案:

答案 0 :(得分:2)

使用explode()并执行以下操作:

foreach ($filtered as $hotel) {     
    $xmlhotels[] = array(        
        'image'=>explode('|', $hotel->imageref),
        'villaname'=>(string)$hotel->hotelname
    );
}

foreach ($myhotels as $villa) {    
    echo $villa['villaname'];
    echo $villa['image'][0];
}