algorithm - Simple number distribution in array php
one text
Solution:
You can do it like below :
<?php
function getArray($fill,$noOfItem){
$maxIndex = floor($noOfItem/$fill);
$rang = range(1,20);
$finalArray = [];
foreach($rang as $key => $val){
if($val % $maxIndex == 0 && array_sum($finalArray) < $fill){
$finalArray[] =1;
}else{
$finalArray[] =0;
}
}
print_r($finalArray);
}
getArray(6,20);
getArray(8,20);
getArray(19,20);
Output: https://3v4l.org/QY6HQ And https://3v4l.org/gQeHa (for a bit uniformity)
Note: Uniformity of 0 and 1 is not 100% guarantee in above answer.
Source