PHP如何从具有唯一随机结果的数组制作子数组?

时间:2018-11-27 18:41:35

标签: php arrays random

我的脚本如下:

<?php 
$placeholders = 'City1 - City1 - City1 - City2 - City3 - City4 - City4';
$substitutes  = [
'City1' => ['Orlando','Dallas','Atlanta','Detroit','Houston'],
'City2' => ['Jakarta','Bandung','Surabaya','Medan','Makassar','Surabaya'],
'City3' => ['Atlanta','Tampa','Miami','Houston'],
'City4' => ['Mandalay','Caloocan','Hai Phong','Quezon City'],
];
$replacements = [];
foreach($substitutes as $key => $choices) {
    $random_key = array_rand($choices);
    $replacements[$key] = $choices[$random_key];
}
$spun = str_replace(
    array_keys($replacements),
    array_values($replacements),
    $placeholders
);
echo $spun;
?>

但是我得到的结果是相同的...

结果:奥兰多-奥兰多-奥兰多-万隆-迈阿密-卡洛坎-卡洛坎

我想要这样的结果:

结果:奥兰多-休斯敦-亚特兰大-万隆-迈阿密-卡洛坎-曼德勒

请给我一个示例代码。

0 个答案:

没有答案