duanchai0028 2013-02-15 04:07
浏览 58
已采纳

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

I am building a space-related game and do not know how to loop through my array of percentage "spawn" chances, to end up with one generated star with a certain size.

Example of how it must generate:

$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. Red, Orange and Yellow stars each have a spawn chance, only one can spawn at a time
  2. For each spawned star there is a chance of what size it shall become
  3. The equation needs to accommodate for possibly 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",
        )
    );
    
  • 写回答

1条回答 默认 最新

  • dongshuzui0335 2013-02-15 04:20
    关注

    Should not be a problem like this:

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

    If you need the keys you can use:

    $star_array;
    
    foreach($star_array as $star => $chances_array) {
      foreach($chances_array as $chance_name => $chance_percentage) {
        // do sth
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测