1

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
)
4
  • Would one of the statistics functions be what you are looking for? php.net/manual/en/ref.stats.php Commented Mar 12, 2021 at 17:24
  • The number 6 goes into 20 3 times with a remainder of 2 so I guess every third with the remainder at the end would work? As for 19, I'd fill all 1s until the final 0 representing the remainder 1. You might also have to account for numbers over 20? Your examples appear to put remainders at the start. Not sure if that was intended. You could split remainders 2 or more to have some on each end but it'll be a little asymmetrical for odd remainders. Commented Mar 12, 2021 at 17:27
  • Unclear. What's the logic behind this distribution? Is it random? Commented Mar 12, 2021 at 17:46
  • Of course, you can't fill in 18 ones in a size of 20 uniformly.. I believe it's true with any 20 % $fill != 0 Commented Mar 12, 2021 at 17:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

hello, how can you give that same logic with 12 and above ?
@israel didn't get you
if i put $fill above 10, i got sequences anyway, i solved changing 12 to remainder, in that case is 8, then i keep it below 10 and swapping the "0,1" filling logic works. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.