0

PHP 配列から 2 つの順序付けられていないリストを作成しようとしています。探しているものとほぼ同じこのスレッドを見つけましたが、最初のリストには 11 個の項目があり、2 番目のリストには残りの項目が必要です。これが私のコードです:

<?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; ?>
4

3 に答える 3

2

array_slice() を使用して、配列を 11 項目に分割し、残りを分割する方法を次に示します。

$firsthalf = array_slice($rows, 0, 11);
$secondhalf = array_slice($rows, 11);
于 2013-01-20T23:31:49.893 に答える
1

array_sliceのドキュメントを見ると、分割のサイズを 3 番目のパラメーターとして指定し、2 番目のパラメーターはオフセットであることがわかります。

<?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
?>
于 2013-01-20T23:33:47.793 に答える
1
// $items = count($rows);
// $split = ceil($items/2);
$firsthalf = array_slice($rows, 0, 11);
$secondhalf = array_slice($rows, 11);
于 2013-01-20T23:34:49.717 に答える