0

我正在阅读一个带有以下内容的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
    );
}

当我回显 Villaname foreach 值时,我得到了它。

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

如何仅回显 xml 文件中的第一张图像 (image1.jpg)。

4

1 回答 1

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];
}
于 2013-10-14T17:27:35.400 回答