2015-12-02 84 views
1

我想隨機化x(取決於$ max)結果。現在,該列表按照代碼指向的XML文件($ string)的順序呈現。有任何想法嗎?隨機化SimpleXML對象結果

<?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>&nbsp;&nbsp; 
      <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

如果您的XML沒有很多的項目,那麼你就可以在內存中通過使用第二陣列和PHP函數洗牌對它們進行排序:

<?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>&nbsp;&nbsp; 
     <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

感謝巴勃羅!這有幫助! – Gerard