PHP - 通过多维百分比列表循环

时间:2013-02-15 04:07:44

标签: php random multidimensional-array percentage

我正在构建一个与太空相关的游戏,并且不知道如何循环我的百分比“产生”机会,最终得到一颗具有一定大小的恒星。

必须如何生成的示例:

$star_chance = rand(1,100);
$star_size_chance = rand(1,100);
//loop through the list of stars here
//selected star - orange
    //loop through the size chances of star orange
    //selected star size = medium
  1. 红色,橙色和黄色星星每个都有一个产卵机会,一次只能生成一个
  2. 对于每个产生的恒星,它有可能变成它的大小
  3. 等式需要适应更多的恒星或尺寸

    $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",
        )
    );
    

1 个答案:

答案 0 :(得分:1)

不应该是这样的问题:

$star_array;

foreach($star_array as $chances_array) {
  foreach($chances_array as $chance) {
    // do sth
  }
}

如果您需要可以使用的按键:

$star_array;

foreach($star_array as $star => $chances_array) {
  foreach($chances_array as $chance_name => $chance_percentage) {
    // do sth
  }
}