我想在下面随机化x(取决于$ max)结果。现在,列表按代码所指向的XML文件的顺序显示($ string)。有任何想法吗?
<?php
$xml_feed = simplexml_load_string($string);
global $xml_global_f;
$xml_global_f = $xml_feed;
foreach($xml_global_f->reviewDetails->reviewDetail as $val)
{
if ($count < $max)
{
?>
<li><p><i class="fa fa-comment-o fa-flip-horizontal fa-lg"></i>
<span itemprop="review" itemscope itemtype="http://schema.org/Review">
<span itemprop="name"><strong><?php
echo $val->user; ?></strong></span><br /><br>
<span itemprop="reviewBody"> <?php
echo $val->text; ?></span>
<span content="<?php
echo $val->user; ?>" itemprop="author"></span><span content="<?php
echo $global_fcs_bn; ?>" itemprop="itemReviewed"></span></p></li>
<?php
}
$count++;
}
?>
答案 0 :(得分:0)
如果您的xml没有很多项目,那么您可以使用第二个数组和php函数shuffle在内存中对它们进行排序:
<?php
$xml_feed = simplexml_load_string($string);
global $xml_global_f;
$xml_global_f = $xml_feed;
foreach($xml_global_f->reviewDetails->reviewDetail as $val)
$array[]= $val;
shuffle($array);
foreach($array as $val)
{
if ($count < $max)
{
?>
<li><p><i class="fa fa-comment-o fa-flip-horizontal fa-lg"></i>
<span itemprop="review" itemscope itemtype="http://schema.org/Review">
<span itemprop="name"><strong><?php
echo $val->user; ?></strong></span><br /><br>
<span itemprop="reviewBody"> <?php
echo $val->text; ?></span>
<span content="<?php
echo $val->user; ?>" itemprop="author"></span><span content="<?php
echo $global_fcs_bn; ?>" itemprop="itemReviewed"></span></p></li>
<?php
}
$count++;
}
?>