我有一个带有内爆的array_rand输出问题,我已经停留了2天。任何帮助都会很感激。
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[$key] = $list[$key];
}
return $random;
}
$keywords_array = array("car 1 "," car 2 "," car 3 "," car 4 "," car 5 "," car 5 "," car 6");
$frontword_list = array("Amazing", "Good-Looking", "Magnificent", "Pretty", "Sparkling", "Awesome", "Imaginative", "Outstanding");
$frontword_list = $frontword_list[array_rand($frontword_list)];
$preposition = array(" alongside "," amidst "," among ", " beside "," including "," near ", " as well "," next to ", " within ");
$preposition_rand = $preposition[array_rand($preposition)];
$description = $frontword_list ." ". implode($preposition_rand, shuffle_assoc($keywords_array));
echo $description;
$preposition_rand
的输出不会随机播放
Amazing car 2 next to car 6 next to car 5 next to car 3 next to car 5 next to car 1 next to car 4
将$preposition_rand
输出改为
Amazing car 2 amidst car 6 including car 5 alongside car 3 beside car 5 near car 1 as well car 4
答案 0 :(得分:0)
你可以试试这个:
<?php
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[$key] = $list[$key];
}
return $random;
}
//a new function that returns the string according to the requirements
function implode_array($prep,$keywords_arr) {
$string = "";
for ( $num = 0; $num < count($keywords_arr); $num++ ) {
$random_index = rand(0,count($prep)-1); //set random number, random number does not exceed the length of $preposition
if ( $num != count($keywords_arr)-1 ) {
$string .= $keywords_arr[$num] . $prep[$random_index]; //insert preposition after keywords_array[$num] if keyword isn't last of array
} else {
$string .= $keywords_arr[$num]; //does not insert preposition after the last index in keywords_array
}
unset($prep[$random_index]); //unset used index
$prep = array_values($prep); //reset index values
}
return $string;
}
$keywords_array = array("car 1 "," car 2 "," car 3 "," car 4 "," car 5 "," car 5 "," car 6");
$frontword_list = array("Amazing", "Good-Looking", "Magnificent", "Pretty", "Sparkling", "Awesome", "Imaginative", "Outstanding");
$frontword_list = $frontword_list[array_rand($frontword_list)];
$preposition = array(" alongside "," amidst "," among ", " beside "," including "," near ", " as well "," next to ", " within ");
$description = $frontword_list ." ". implode_array($preposition,$keywords_array); //call the new function
echo $description;
显示如下内容:
Outstanding car 1 next to car 2 beside car 3 near car 4 amidst car 5 as well car 5 within car 6
如果你有更多的keywords_array而不是你的介词,你可以删除这些行:
unset($prep[$random_index]); //unset used index
$prep = array_values($prep); //reset index values
答案 1 :(得分:0)
在这一点上,我的拙见认为你错误地使用了array_rand $ preposition_rand = $ preposition [array_rand($ preposition)];
你可以尝试这样的事情:
function join_list($a,$b){
return "$a $b";
}
$keywords_array = array("car 1 "," car 2 "," car 3 "," car 4 "," car 5 "," car 5 "," car 6");
$frontword_list = array("Amazing", "Good-Looking", "Magnificent", "Pretty", "Sparkling", "Awesome", "Imaginative", "Outstanding");
$frontword_list = $frontword_list[array_rand($frontword_list)];
$preposition = array(" alongside "," amidst "," among ", " beside "," including "," near ", " as well "," next to ", " within ");
shuffle($preposition);
$preposition_rand = array_slice($preposition,0, count($keywords_array)-1);
$description = $frontword_list ." ". implode("",array_map("join_list", $keywords_array, $preposition_rand));
echo $description;
echo "\n";
输出 - 随机变化: 汽车2旁边的汽车2旁边的汽车2和汽车5旁边的汽车4以及包括汽车6的汽车5