for循环查看xml输出php

时间:2018-03-09 10:19:23

标签: php xml xml-parsing

我试图在php输出中显示XML值。一切都按预期工作,除了下面的图像是我为实现PHP输出

所做的代码
    <table>
    <thead>
    <th>Property Ref No</th>
    <th>Property title</th>
    <th>Property type</th>
    <th>Property Description</th>
    <th>Bathroom</th>
    <th>Bedroom</th>
    <th>Price</th>
    <th>Property created on</th>
    <th>Property updated on</th>
    <th>Property Images</th>
    </thead>
<?php
$url    = 'http://api.pafilia.com/feed/datafeed?resales=1&lang=EN&limit=5000';
$html   = file_get_contents($url);
$invalid_characters = '/[^\x9\xa\x20-\xD7FF\xE000-\xFFFD]/';
$html = preg_replace($invalid_characters, '', $html);
$xml = simplexml_load_string($html);
/*
//test purpose part 
$encode = json_encode($xml);
$decode = json_decode($encode, true);
print_r($decode)
*/
foreach($xml->properties->property  as $properties)
{
?>
<tr>
<td> <?php  echo $properties->propertyref; ?></td>
<td><?php echo $properties->propertyname; ?></td>
<td> <?php  echo $properties->propertyType; ?></td>
<td> <?php  echo $properties->description .'<br/>'; 
echo "Features :".'<br/>';
echo $properties->features; ?>
</td>
<td> <?php  echo $properties->bathrooms; ?></td>
<td> <?php  echo $properties->bedrooms; ?></td>
<td> <?php  echo $properties->price; ?></td>
<td> <?php  echo $properties->propertyCreated; ?></td>
<td> <?php  echo $properties->propertyUpdated; ?></td>
<td> 
<?php
foreach($properties->media->mediagroup->mediaitem  as $mediagroup){
    echo $mediagroup->link;
    }
?>
</td>
</tr>
<?php   } ?>
<tbody>
</tbody>
</table> 

我没有得到的是我每个<link type="largephoto-link">只需要大图片<link type="largephoto-link">,但我得到的是一些图片。我需要单独显示大图像。在一个<td>中,并且需要进入其他<td>。有没有办法实现这样的目标。

以下是我使用http://api.pafilia.com/feed/datafeed?resales=1&lang=EN&limit=5000

的XML

1 个答案:

答案 0 :(得分:2)

您可以使用['type']来比较属性:

foreach($properties->media->mediagroup->mediaitem  as $mediagroup) {
    foreach ($mediagroup->link as $link) {
        if ((string)$link['type'] == 'largephoto-link') {
            echo (string) $link;
        }
    }
}