php函数将数组拆分为3个部分而不留下余数

时间:2015-07-10 08:52:31

标签: php arrays

我有一个简单的数组,我想分成3个随机行。

到目前为止,我正在使用数组块函数,该函数成功将其拆分为3行,但由于数组计数不均匀,因此将数组转换为3行,但剩余2个值为一行4th行。请参阅下面的代码......

<?php

  $my_bikes = array (
    'bike-996.png',
    'bike-adventure.png',
    'bike-bandit.png',
    'bike-blade.png',
    'bike-classic-racer.png',
    'bike-classic-rm.png',
    'bike-custom.png',
    'bike-dragster.png',
    'bike-ducati-scrambler.png',
    'bike-electric-concept.png',
    'bike-flat-tracker.png',
    'bike-goldwing.png',
    'bike-gsx.png',
    'bike-harley.png',
    'bike-indian-vintage.png',
    'bike-kiddimoto.png',
    'bike-livewire.png',
    'bike-lotus.png',
    'bike-monkey-bike.png',
    'bike-moto3.png',
    'bike-mx.png',
    'bike-rc.png',
    'bike-rc213v.png',
    'bike-roland-sands.png',
    'bike-scooter.png',
    'bike-speedway.png',
    'bike-tron.png',
    'bike-vespa.png',
    'bike-vision.png',
  );

  shuffle($my_bikes);

  // chunk my bike
  $my_bikes = array_chunk($my_bikes, count($my_bikes) / 3);

?>

<?php foreach($my_bikes as $key => $my_bike_row) : ?>

  <div class="row <?=$key % 2==0 ? 'left' : 'right' ?>">
    <?php foreach($my_bike_row as $my_bike): ?>
      <img src="<?=get_path('images/bikes/' . $my_bike)?>" alt="">
    <?php endforeach; ?>
  </div>

<?php endforeach; ?>

<?=exit( '<pre>' . print_r( $my_bikes, true ));?>

请参阅下面的当前输出...

Array
(
    [0] => Array
        (
            [0] => bike-roland-sands.png
            [1] => bike-monkey-bike.png
            [2] => bike-classic-rm.png
            [3] => bike-classic-racer.png
            [4] => bike-vespa.png
            [5] => bike-rc213v.png
            [6] => bike-flat-tracker.png
            [7] => bike-dragster.png
            [8] => bike-custom.png
        )

    [1] => Array
        (
            [0] => bike-speedway.png
            [1] => bike-blade.png
            [2] => bike-tron.png
            [3] => bike-indian-vintage.png
            [4] => bike-harley.png
            [5] => bike-goldwing.png
            [6] => bike-kiddimoto.png
            [7] => bike-livewire.png
            [8] => bike-moto3.png
        )

    [2] => Array
        (
            [0] => bike-adventure.png
            [1] => bike-mx.png
            [2] => bike-rc.png
            [3] => bike-scooter.png
            [4] => bike-996.png
            [5] => bike-gsx.png
            [6] => bike-ducati-scrambler.png
            [7] => bike-lotus.png
            [8] => bike-electric-concept.png
        )

    [3] => Array
        (
            [0] => bike-vision.png
            [1] => bike-bandit.png
        )

)

如您所见,我留下了4行。

我的问题是,PHP有哪些方法我可以将数组拆分为3行,但如果它的数字不均匀(根据我的例子),最后的剩余数据包含在3行中,而不是离开了。

理想情况下,我不会有任何重复。

由于

2 个答案:

答案 0 :(得分:7)

使用array_chunk()

通过将分数(如果有的话)四舍五入到ceil(count($my_bikes)/3)

的下一个最高整数值来计算大小

所以在这种情况下 - count($my_bikes)/3 =&gt; 26/3 =&gt; 8.66

ceil(8.66) =&gt; 9

块大小为9, 9, 8

array_chunk($my_bikes, (ceil(count($my_bikes)/3)))

答案 1 :(得分:0)

 array_chunk($my_bikes,(count($my_bikes)/3)+1);