random - PHP - Looping through a mutidimensional list of percentages -
i building space-related game , not know how loop through array of percentage "spawn" chances, end 1 generated star size.
example of how must generate:
$star_chance = rand(1,100); $star_size_chance = rand(1,100); //loop through list of stars here //selected star - orange //loop through size chances of star orange //selected star size = medium
red, orange , yellowish stars each have spawn chance, 1 can spawn @ time for each spawned star there chance of size shall become the equation needs accommodate perchance more stars or sizes
$star_array = array( "red" => array( "spawn_chance" => "25", "size_immense_chance" => "50", "size_giant_chance" => "25", "size_large_chance" => "15", "size_medium_chance" => "5", "size_small_chance" => "4", "size_dwarf_chance" => "1" ), "orange" => array( "spawn_chance" => "50", "size_immense_chance" => "4", "size_giant_chance" => "15", "size_large_chance" => "50", "size_medium_chance" => "25", "size_small_chance" => "6", "size_dwarf_chance" => "0" ), "yellow" => array( "spawn_chance" => "25", "size_immense_chance" => "0", "size_giant_chance" => "6", "size_large_chance" => "25", "size_medium_chance" => "50", "size_small_chance" => "15", "size_dwarf_chance" => "4", ) );
should not problem this:
$star_array; foreach($star_array $chances_array) { foreach($chances_array $chance) { // sth } }
if need keys can use:
$star_array; foreach($star_array $star => $chances_array) { foreach($chances_array $chance_name => $chance_percentage) { // sth } }
php random multidimensional-array percentage
No comments:
Post a Comment