Php 排序和输出问题
Im使用下面的PHP生成一些HTML输出:Php 排序和输出问题,php,Php,Im使用下面的PHP生成一些HTML输出: <?php $url = "images.xml"; $xmlstr = file_get_contents($url); $xml = new SimpleXMLElement($xmlstr); $images = array(); $ids = array(); foreach ($xml->image as $image) { $images[]['id'] = $image -> id; $imag
<?php
$url = "images.xml";
$xmlstr = file_get_contents($url);
$xml = new SimpleXMLElement($xmlstr);
$images = array();
$ids = array();
foreach ($xml->image as $image) {
$images[]['id'] = $image -> id;
$images[]['link'] = $image->href;
$images[]['src'] = $image->source;
$images[]['title'] = $image->title;
$images[]['alt'] = $image->alt;
$ids[] = $image -> id;
}
array_multisort($ids, SORT_ASC, $images);
foreach ($images as $image){
echo "<a href='".$image['link']."'><img src='".$image['src']."' alt='".$image['alt']."' title='".$image['title']."' /></a>";
}
?>
我得到了3次图像链接,这是正确的,因为XML中有3条记录。但我有12份文本
为什么会发生这种情况?您正在将每个属性放入数组中的新行中。 试试这个:
foreach ($xml->image as $image)
{
$images[] = array(
'id' => $image->id,
'link' => $image->href,
'src' => $image->source,
'title' => $image->title,
'alt' => $image->alt
);
$ids[] = $image -> id;
}
将每个属性放入数组中的新行中。 试试这个:
foreach ($xml->image as $image)
{
$images[] = array(
'id' => $image->id,
'link' => $image->href,
'src' => $image->source,
'title' => $image->title,
'alt' => $image->alt
);
$ids[] = $image -> id;
}
我认为您需要对错误的输出更加精确一点——可能您的XML片段也会有所帮助。据我所知,您是说相同的
for
循环运行3次或12次(或者可能同时运行两次?!?)。下面大家都明白了。我认为您需要对错误的输出更加精确一点——可能您的XML片段也会有所帮助。据我所知,你是说相同的for
循环运行3次或12次(或者可能两者都运行?!?),其他人都明白了。