Lasser
Lasser

Reputation: 73

add values to array by iterate one at a time

I am trying to get this function working by adding one point at at time, till there are no more. What I am trying to do is, getting it to increment "Torsk" value by one, then go to the next, increment by one, and then so on an on, till all the points set in $prize have been distributed.

$prize = [
    "Torsk" => 0,
    "Rødspætte" => 0,
    "Mørksej" => 0,
    "Skrubbe" => 0,
    "Lubbe/Lyssej" => 0,
    "Ising" => 0,
    "Lange" => 0,
    "Makrel" => 0,
    "Hornfisk" => 0,
    "Pig-/Slet-hvar" => 0,
    "Havkat" => 0,
    "Laks/Havørred" => 0,
];

$points = 21;

for ($i = 0; $i < $points; ++$i) {

    foreach ($prize as $key => $value) {

      $prize[$key]++;

      if (array_sum($prize) == $points) continue;

    }
}

echo array_sum($prize);
echo "<br />";
print_r($prize);

When it is finished I expected the array to be like this:

$prize = [
    "Torsk" => 2,
    "Rødspætte" => 2,
    "Mørksej" => 2,
    "Skrubbe" => 2,
    "Lubbe/Lyssej" => 2,
    "Ising" => 2,
    "Lange" => 2,
    "Makrel" => 2,
    "Hornfisk" => 2,
    "Pig-/Slet-hvar" => 1,
    "Havkat" => 1,
    "Laks/Havørred" => 1,
];

But it is not, instead it has just added 21 to all of them.

Upvotes: 1

Views: 49

Answers (3)

Kerkouch
Kerkouch

Reputation: 1456

You should use break 2; instead of continue to break the two nested enclosing structures.

The following is an improved way to do the same task.

<?php


/**
 * Disturbe values until the sum is equal to $points
 * @param $prize array
 * @param $points integer
 * @return array|false
 */
function distribute_prize( $prize = array(), $points = 21 ){

    $sum = array_sum( $prize );
    if( $sum == $points ) return $points;
    elseif( $sum > $points || $points < 0 ) return false;

    while( true ){
        foreach ($prize as $key => $value) {
            $prize[$key]++;
            if( array_sum( $prize ) == $points ) break 2;

        }
    }

    return $prize;
}

// TEST
$prize = [
    "Torsk" => 0,
    "Rødspætte" => 0,
    "Mørksej" => 0,
    "Skrubbe" => 0,
    "Lubbe/Lyssej" => 0,
    "Ising" => 0,
    "Lange" => 0,
    "Makrel" => 0,
    "Hornfisk" => 0,
    "Pig-/Slet-hvar" => 0,
    "Havkat" => 0,
    "Laks/Havørred" => 0,
];

$points = 21;
print_r( distribute_prize( $prize, $points ) );

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

you could use break 2; to break from outer loop when your condition is reached, like:

for ($i = 0; $i < count($prize); ++$i) {

    foreach ($prize as $key => $value) {

      $prize[$key]++;

      if (array_sum($prize) == $points) {
          break 2;
      }

    }
}

Upvotes: 3

Amit Merchant
Amit Merchant

Reputation: 1043

Try this code snippet:

$prize = [
        "Torsk" => 0,
        "Rødspætte" => 0,
        "Mørksej" => 0,
        "Skrubbe" => 0,
        "Lubbe/Lyssej" => 0,
        "Ising" => 0,
        "Lange" => 0,
        "Makrel" => 0,
        "Hornfisk" => 0,
        "Pig-/Slet-hvar" => 0,
        "Havkat" => 0,
        "Laks/Havørred" => 0,
];

$points = 21;

foreach ($prize as $key => $value) {

    $prize[$key]++;

    if (array_sum($prize) == $points) break;

}

if(array_sum($prize) < $points){
    foreach ($prize as $key => $value) {

        $prize[$key]++;

        if (array_sum($prize) == $points) break;

    }    
}   

echo array_sum($prize);
echo "<br />";
print_r($prize);

Upvotes: 0

Related Questions