# Simple number distribution in array php

I want to distribute in array to get homogeneous distribution.

I created an array with array_fill, and set a variable \$fill with a valor up to 20 to distribute in that array.

What I need to get as result – for example:

```\$fill = 5;

// returns
Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 1
[4] => 0
[5] => 0
[6] => 0
[7] => 1
[8] => 0
[9] => 0
[10] => 0
[11] => 1
[12] => 0
[13] => 0
[14] => 0
[15] => 1
[16] => 0
[17] => 0
[18] => 0
[19] => 1
)

\$fill = 10;

// returns
Array (
[0] => 0
[1] => 1
[2] => 0
[3] => 1
[4] => 0
[5] => 1
[6] => 0
[7] => 1
[8] => 0
[9] => 1
[10] => 0
[11] => 1
[12] => 0
[13] => 1
[14] => 0
[15] => 1
[16] => 0
[17] => 1
[18] => 0
[19] => 1
)
```

What should I do for, \$fill = 6, 18 or even 19 ?

Edit 1:

The remainders I don’t have preference to start or end, I need to fill the array with at least fair distribution, so when I foreach the array, I don’t get a long sequence of 1 or 0.

I tried this

```\$fill = 5;
\$arr = array_fill(0,20,0);
\$gap = 20 / \$fill;
foreach(\$arr as \$k=>\$v) {
if( is_int(\$k / \$gap) ) {
\$arr[\$k] = 1;
}
}

print_r(\$arr);
```

but it doesn’t work with 8,12 or 18

Edit 2:

Sorry for the complication, I just need to fill the array with all 5,10 or 18 ‘1’s, don’t necessary the same quantity for zeros, but at least this:

```\$fill = 18;

// returns
Array (
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1
[5] => 0
[6] => 1
[7] => 1
[8] => 1
[9] => 1
[10] => 1
[11] => 1
[12] => 1
[13] => 1
[14] => 1
[15] => 0
[16] => 1
[17] => 1
[18] => 1
[19] => 1
)
```

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.