我有一系列的图像.每张图片都有一个"稀有"键,告诉我它是"常见","不常见"还是"罕见".例如,数组可能看起来像这样:
Array
(
[0] => Array
(
[image] => photo1.jpg
[rarity] => common
)
[1] => Array
(
[image] => photo2.jpg
[rarity] => uncommon
)
.
.
.
[x] => Array
(
[image] => photo(x).jpg
[rarity] => rare
)
)
Run Code Online (Sandbox Code Playgroud)
我想从列表中选择'y'个图像,几乎就像创建一副牌,但是用图像代替.当然,稀有性定义了选择卡的可能性.我该怎么做呢?我想我是从array_rand()开始的,但我仍然坚持要去哪里.
编辑澄清:阵列中的每个图像只能出现在最后一层中.
我会用三个不同的数组做到这一点.每个稀有类型一个.
让我们说概率是:普通= 70%,罕见= 25%,罕见= 5%.
你的三个数组是$commonArray,$uncommonArray和$rareArray.
你的目标牌组是$deck.
然后生成1到100之间的随机数并选择三个数组中的一个:
<?php
$rand = rand(1,100);
if ($rand <= 70) {
$cardArray = &$commonArray;
} else if ($rand <= 95) {
$cardArray = &$uncommonArray;
} else {
$cardArray = &$rareArray;
}
Run Code Online (Sandbox Code Playgroud)
现在从所选数组中选择一张卡片:
$chosen = array_rand($cardArray);
$deck[] = $cardArray[$chosen];
unset($cardArray[$chosen]); //remove the chosen card from the array to prevent duplicates
Run Code Online (Sandbox Code Playgroud)
重复此操作,直到您$deck想要它的大小.
| 归档时间: |
|
| 查看次数: |
904 次 |
| 最近记录: |