Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php function to split array into 3 parts with out leaving remainders

Tags:

arrays

php

I have an simple array that I would like split into 3 random rows.

So far I am using the array chunk function which successful splits it into 3 rows, but because the array count is uneven, it spilts the array into 3 rows but leaves a remainder 2 values in a 4th row. See code below...

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

See my current output below...

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
        )

)

As you can see, I'm left with 4 rows.

My question is, what method is there with PHP where I can split the array into 3 rows, but if its an uneven number (as per my example) that the last the remainders are included the in the 3 rows, and not left remainding.

I don't wan't any duplicates ideally.

Thanks

like image 342
joshmoto Avatar asked Dec 14 '22 13:12

joshmoto


1 Answers

Use array_chunk()

And calculate the size by rounding up the fraction(if any) to next highest integer value by ceil(count($my_bikes)/3)

So in this case - count($my_bikes)/3 => 26/3 => 8.66.

ceil(8.66) => 9

The chunk sizes will be 9, 9, 8

array_chunk($my_bikes, (ceil(count($my_bikes)/3)))
like image 185
Sougata Bose Avatar answered Dec 25 '22 11:12

Sougata Bose