As I am trying to describe in my code below. I am trying to build a new array, based on the stats. When it goes through the keys, it should add one in value to the key in $prize, and then continue, until 21 points are given. If it finds a stat key with value 0, it should just continue on to next iteration.
Now here comes my issue. Because in the example below, when it hits the "Laks/Havørred" => 0,
in $stats
, it should just skip the iteration, and then start from the top again being"Torsk" => 3.
Instead it adds the point to "Pig-/Slethvar" => 2
, rather randomly. Can I somehow overcome this issue, or force the order it handles the data?
$stats = [
“Torsk” => 3,
“Rødspætte” => 5,
“Mørksej” => 5,
“Skrubbe” => 5,
“Lubbe/Lyssej” => 5,
“Ising” => 2,
“Lange” => 5,
“Makrel” => 5,
“Hornfisk” => 1,
“Pig-/Slethvar” => 5,
“Havkat” => 5,
“Laks/Havørred” => 0,
];$prize = [
“Torsk” => 0,
“Rødspætte” => 0,
“Mørksej” => 0,
“Skrubbe” => 0,
“Lubbe/Lyssej” => 0,
“Ising” => 0,
“Lange” => 0,
“Makrel” => 0,
“Hornfisk” => 0,
“Pig-/Slethvar” => 0,
“Havkat” => 0,
“Laks/Havørred” => 0,
];$points = 21;
for ($i = 0; $i < count($prize); ++$i) {
foreach ($prize as $key => $value) { $prize[$key]++; if (array_sum($prize) == $points) { break 2; } }
}
foreach ($prize as $key => $val) {
$prize_array[] = ['art' => $key, 'antal' => $val];
}
echo json_encode($prize_array);