Php 阵列随机和内爆输出卡滞

Php 阵列随机和内爆输出卡滞,php,Php,我的array_rand输出有一个内爆问题,我已经卡住了2天。任何帮助都将不胜感激 函数shuffle\u assoc($list){ 如果(!is_数组($list))返回$list; $keys=数组_键($list); 洗牌($键); $random=array(); foreach($key作为$key){ $random[$key]=$list[$key]; } 返回$random; } $keywords_数组=数组(“车1”、“车2”、“车3”、“车4”、“车5”、“车5”、“车

我的array_rand输出有一个内爆问题,我已经卡住了2天。任何帮助都将不胜感激

函数shuffle\u assoc($list){ 如果(!is_数组($list))返回$list; $keys=数组_键($list); 洗牌($键); $random=array(); foreach($key作为$key){ $random[$key]=$list[$key]; } 返回$random; } $keywords_数组=数组(“车1”、“车2”、“车3”、“车4”、“车5”、“车5”、“车6”); $frontword_list=数组(“惊人”、“好看”、“华丽”、“漂亮”、“闪闪发光”、“很棒”、“富有想象力”、“杰出”); $frontword_list=$frontword_list[array_rand($frontword_list)]; $prepose=数组(“旁边”、“中间”、“中间”、“旁边”、“包括”、“附近”、“以及”、“旁边”、“内部”); $preposition_rand=$preposition[array_rand($preposition)]; $description=$frontword_list.”。内爆($preposition_rand,shuffle_assoc($keywords_array)); 回声$描述;
$preposition\u 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\u rand
输出的正确代码是什么

Amazing car 2 amidst car 6 including car 5 alongside car 3 beside car 5 near car 1 as well car 4 
您可以尝试以下方法:

<?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;
如果您的关键字数组多于您的介词,您可以删除以下行:

unset($prep[$random_index]); //unset used index
$prep = array_values($prep); //reset index values

在这一点上,我的拙见是你使用了错误的数组 $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";
输出-随机变化:
华丽的1号车与2号车并排,3号车与4号车并排,5号车与5号车(包括6号车)

内爆采用单个字符串参数。并在所有内爆实例中使用它——因此得到的结果。这是一个有趣的问题。我想知道您是否需要array_reduce()和自定义回调函数,或者array_replace()?非常感谢,您帮了我很多。我非常欣赏你的函数$random_index=rand(0,count($prep)-1)//设置随机数,随机数不超过$prepose的长度,如果关键字_arr超过$prepose的长度,如何设置?也就是说,如果我有一个超过10个的关键字,而$prepose只有5个,那么关键字之间的输出从6到10,没有介词吗?我们如何解决这个问题?再次感谢您,您可以通过删除unset($prep[$random_index])来修复此问题//未设置已用索引和$prep=数组_值($prep)//重置索引值EAH。。谢谢你,你又救了我一命。我必须学习更多关于php的知识。谢谢,没问题。我是来帮忙的:拇指支撑:
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";