我在这个阵列中有12个项目:
$item[1] = array('pseudo' => 'Item1', 'name' => 'apple', 'rarity' => '1', 'id' => '1' );
$item[2] = array('pseudo' => 'Item2', 'name' => 'banana', 'rarity' => '30', 'id' => '8' );
$item[3] = array('pseudo' => 'Item3', 'name' => 'cherry', 'rarity' => '23', 'id' => '12' );
$item[4] = array('pseudo' => 'Item4', 'name' => 'pear', 'rarity' => '27', 'id' => '18' );
$item[5] = array('pseudo' => 'Item5', 'name' => 'watermelon', 'rarity' => '70', 'id' => '14' );
$item[6] = array('pseudo' => 'Item6', 'name' => 'orange', 'rarity' => '100', 'id' => '17' );
我需要的是随机打印6件物品中的1件但受其稀有度的影响,所以越罕见的是它发生的次数越少,稀有度为1-100。我如何使用PHP实现这一目标?
答案 0 :(得分:0)
您可以根据它的稀有程度将每个项目键的x个副本添加到数组中。例如:
$pool = array();
foreach ($items as $key => $value) {
// Subtract the items rarity from 101,
// generating 100 occurences for the most common
// and 1 occurence for the most rare
$occurrences = 101 - $value['rarity'];
for ($i = 0; $i < $occurrences; $i++) {
// Add the items key $occurrences times
array_push($pool, $key);
}
}
$weighed_item = $items[array_rand($pool)];