Php:字符串索引不一致?

Php:字符串索引不一致?,php,arrays,Php,Arrays,我创建了一个函数,可以从硬编码的单词列表中随机生成一个短语。我有一个函数get_words(),它有一个硬编码的单词字符串,它将其转换成一个数组,然后洗牌并返回 get\u words() 我的问题是,出于某种原因,PHP总是给我不一致的结果。它确实给了我一些随机的单词,但它给出的单词数量不一致。我指定4个单词作为默认值,它给我的短语范围是1-4个单词,而不是4个单词。这个程序非常简单,几乎令人难以置信,我无法准确指出问题所在。链中的断开链接似乎是正在索引的$words数组,似乎出于某种原因,有

我创建了一个函数,可以从硬编码的单词列表中随机生成一个短语。我有一个函数
get_words()
,它有一个硬编码的单词字符串,它将其转换成一个数组,然后洗牌并返回

get\u words()

我的问题是,出于某种原因,PHP总是给我不一致的结果。它确实给了我一些随机的单词,但它给出的单词数量不一致。我指定4个单词作为默认值,它给我的短语范围是1-4个单词,而不是4个单词。这个程序非常简单,几乎令人难以置信,我无法准确指出问题所在。链中的断开链接似乎是正在索引的
$words
数组,似乎出于某种原因,有时索引失败。我不熟悉PHP,有人能给我解释一下吗

<?php

function generate_random_phrase() {
  $words = get_words();
  $number_of_words = get_word_count();
  $phrase = "";
  $symbols = "!@#$%^&*()";
  echo print_r($phrase);

  for ($i = 0;$i < $number_of_words;$i++) {
  $phrase .= " ".$words[$i];
}


  if (isset($_POST['include_numbers']))
    $phrase = $phrase.rand(0, 9);

  if (isset($_POST['include_symbols']))
    $phrase = $phrase.$symbols[rand(0, 9)];

  return $phrase;
}

function get_word_count() {
  if ($_POST['word_count'] < 1 || $_POST['word_count'] > 9) 
    $word_count = 4; #default
  else
    $word_count = $_POST['word_count'];
  return $word_count;
}

function get_words() {
  $BASE_WORDS = "my sentence really hope you 
    like narwhales bacon at midnight but only 
    ferver where can paper laptops spoon door knobs 
    head phones watches barbeque not say";
  $words = explode(' ', $BASE_WORDS);
  shuffle($words);
  return $words;
}
?>

您的函数似乎有点不一致,因为数组中也包含空格,这就是为什么当您包含空格时,会将它们包含在循环中,这似乎是5个单词(4个实词和一个空格索引)并不正确。您也可以先过滤空格,包括空格

以下是我的意思的视觉表现:

Array
(
    [0] =>             // hello im a whitespace, i should not be in here since im not really a word

    [1] => but
    [2] => 
    [3] => bacon
    [4] => spoon
    [5] => head
    [6] => barbeque
    [7] => 
    [8] => 
    [9] => sentence
    [10] => door
    [11] => you
    [12] => 
    [13] => watches
    [14] => really
    [15] => midnight
    [16] => 
因此,在本例中,当循环它时,包含空格。如果你有很多单词是
5
,你真的没有得到那5个单词,
索引0-4
看起来你只有3个(
1=>但是,3=>培根,4=>汤匙)

以下是修改后的版本:

function generate_random_phrase() {
    $words = get_words();
    $number_of_words = get_word_count();
    $phrase = "";
    $symbols = "!@#$%^&*()";
    $words = array_filter(array_map('trim', $words)); // filter empty words
    $phrase = implode(' ', array_slice($words, 0, $number_of_words)); // no need for a loop
    // this simply gets the array from the first until the desired number of words (0,5 or 0,9 whatever)
    // and then implode, just glues all the words with space
    // so this ensure its always according to how many words you want

    if (isset($_POST['include_numbers']))
    $phrase = $phrase.rand(0, 9);

    if (isset($_POST['include_symbols']))
    $phrase = $phrase.$symbols[rand(0, 9)];

    return $phrase;
}

在$BASE_单词中,您的制表符和新行在分解数组中占据了一个空间,这就是原因。删除换行符和制表符,它将生成正确的答案。即:

$BASE_WORDS = "my sentence really hope you like narwhales bacon at midnight but only ferver where can paper laptops spoon door knobs head phones watches barbeque not say";

单词表中的空格不一致是问题所在

这里有一个修正:

function get_words() {
  $BASE_WORDS = "my|sentence|really|hope|you|
|like|narwhales|bacon|at|midnight|but|only|
|ferver|where|can|paper|laptops|spoon|door|knobs|
|head|phones|watches|barbeque|not|say";
  $words = explode('|', $BASE_WORDS);
  shuffle($words);
  return $words;
}

哇,你真是个天才!谢谢