Split an Array into N Arrays - PHP

前端 未结 11 1787
一向
一向 2020-12-05 07:05

I have an array of 18 values:

$array = array(\'a\', \'b\', \'c\', \'d\', \'e\', \'f\', \'g\', \'h\', \'i\', \'j\', \'k\', \'l\', \'m\', \'n\', \'o\', \'p\',          


        
11条回答
  •  感动是毒
    2020-12-05 07:59

    What you are describing is not what array_chunk is made for. You should use array_slice() and calculate yourself which parts of the array you want to end up as new arrays. (and use a for loop to iterate over your original array)

    Update:

    Some calculations that could help you:

    minimum_fill = floor(array_length / nr_buckets)
    bigger_buckets_amount = array_length - (minimum_fill / nr_buckets)
    

    Algorithm to fill buckets: Loop over the array, fill the first bigger_buckets_amount amount of buckets with (minimum_fill + 1), fill the rest of the buckets with minimum_fill

提交回复
热议问题