重み付けによる抽選ロジック
<?php // 以下の配列から抽選で一つを抽出したい $things = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ]; // 重み付けをする // この設定だと5番目のもの($thingsの'e')が最も出やすい // 0のものは出ない $rate = '10,20,13,24,35,0,0,0,0,5'; $rate_array = explode(',', $rate); $weight = 0; $rated_indexes = []; foreach ($rate_array as $key=> $r) { // 重みが0になっているものは対象外 if (empty($r)) continue; $weight += (int)$r; // 重み付け後のインデックス配列 $rated_indexes[$weight] = $key + 1; } // 抽選 $lot = mt_rand(1, $counter); foreach ($rated_indexes as $key => $index) { if ($lot <= $key) { $hit = $things[$index]; break; } } // 結果表示 var_dump($hit);