2013-01-20 70 views
0

我想從一個PHP數組創建兩個無序列表,我發現this thread這幾乎是我在找的東西,但是我希望第一個列表有11個項目,剩下的第二個清單。這裏是我的代碼:PHP從數組中創建兩個無序列表

<?php if ($rows) : 

    $items = count($rows); 
    $split = ceil($items/2); 
    $firsthalf = array_slice($rows,$split); 
    $secondhalf = array_slice($rows,0,$split); 
?> 

    <div class="tickets"> 

     <div class="col1"> 
     <ul> 
      <?php foreach ($firsthalf as $item) : ?> 
      <li><a href="">test 1</a></li> 
      <?php endforeach; ?> 
     </ul> 
     </div> 

     <div class="col2"> 
     <ul> 
      <?php foreach ($secondhalf as $item) : ?> 
      <li><a href="">test 2</a></li> 
      <?php endforeach; ?> 
     </ul> 
     </div> 

     <div class="clear"></div> 
    </div> 

<?php endif; ?> 
+0

和問題是什麼? –

+0

在你做'array_slice'之前,只需'shuffle'數組。 – sberry

+0

如何使第一個列表有11個項目 – SoulieBaby

回答

2

下面是如何數組分成11個項目,然後剩下的using array_slice()

$firsthalf = array_slice($rows, 0, 11); 
$secondhalf = array_slice($rows, 11); 
+0

非常感謝,那工作:) – SoulieBaby

1

如果你看看array_slice文檔,您可以看到您指定拆分作爲第三個參數的大小,而第二個是偏移:

<?php 
    if ($rows) : 
     $firsthalf = array_slice($rows, 0, 11); // returns 11 rows from the start 
     $secondhalf = array_slice($rows, 11); // returns everything after the 11th row 
?> 
+0

非常感謝(相同的答案,如下所示) - 但感謝文檔去:) – SoulieBaby

1
// $items = count($rows); 
// $split = ceil($items/2); 
$firsthalf = array_slice($rows, 0, 11); 
$secondhalf = array_slice($rows, 11);