1

I have the following script that divides the total by the number of people and stores the results in an array.

Instead of dumping the $result array at the end, I wonder if I could echo out the value of each array item in the for loop as it goes?

// initial conditions
$total = 1001;
$people = 5;

// count what is the minimal value, that all the results will have
$value = floor($total / $people);
$result = array_fill(0, $people, $value);

// distribute missing "+1"s as needed in the result
$overheads = $total - $value * $people;
for ($i = 0; $i < $overheads; $i++) {
    $result[$i]++;
}

// voila...
var_dump($result);

So, the above script should loop out:

200
200
200
200
201
5
  • 1
    foreach ($result as $r) echo $r . "\n"; ? Commented Mar 8, 2017 at 12:33
  • for ($i = 0; $i < $overheads; $i++) { $result[$i]++; echo $result[$i].'\n'; } or am I getting the question wrong? Commented Mar 8, 2017 at 12:33
  • this.lau_ - you should add as an Answer - that worked great! :) Commented Mar 8, 2017 at 12:34
  • You have the solution done in your example...... Commented Mar 8, 2017 at 12:34
  • Take a look at the PHP manual. The echo statement is what you need here. Commented Mar 8, 2017 at 12:38

2 Answers 2

1

Technically speaking, you can't "echo out the value of each array item in the for loop as it goes" because you're not filling the arrays in a loop, you're filling with array_fill(). The loop you have is to work with the remainder. If you echo out there, you'll get partial results. So either you keep your code and echo the values at the end, with a simple line:

foreach ($result as $r) echo "$r\n";

Or you modify your code to fill the arrays in another manner. Which might be interesting. Note that you're forgetting about the modulus operator, %, for the remainder of division. This line:

$overheads = $total - $value * $people;

Could be written simply as:

$overheads = $total % $people;

An idea to fill in a loop:

// initial conditions
$total = 1001;
$people = 5;

$value = floor($total/$people);
$overheads = $total % $people;

foreach (range(1, $people) as $p) {
    $r = $value;
    if ($overheads) {
        # if there is still remainders:
        $r++;
        $overheads--;
    }
    # echo the value and also store it
    echo "$r\n";
    $result[] = $r;
}
Sign up to request clarification or add additional context in comments.

Comments

1
// initial conditions
$total = 1001;
$people = 5;

// count what is the minimal value, that all the results will have
$value = floor($total / $people);
$result = array_fill(0, $people, $value);

// distribute missing "+1"s as needed in the result
$overheads = $total - $value * $people;

$i = 1;
foreach($result as $res)
{

    if ($i < $people){
     echo $res . "\n";
    }else{
        echo $res + 1 . "\n";
    }
   $i++;
}

// voila...

Comments

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.