在数组php中找到单词的位置

时间:2016-04-09 03:02:58

标签: php arrays dynamic

我几乎已经找到了我的短信问题,并将其缩小到一个我似乎无法开始工作的小问题。

以下是我所拥有的:

include('Services/Twilio.php');

/* Read the contents of the 'Body' field of the Request. */
$body = $_REQUEST['Body'];

/* Remove formatting from $body until it is just lowercase
characters without punctuation or spaces. */

$result = rtrim($body);
$result = strtolower($result);
$text = explode(' ',$result);
$keyword = array('dog','pigeon','owl');
$word = array_intersect($keyword,$text);
$key = array_search($word, array_keys($keyword));

$word[$key]();
/*     ^^this is the issue */

因此,短信应用程序现在可以读取一个句子,并找到关键字没有汗水。问题是,我还需要将该单词的位置与关键字数组中的位置相关联。如果我手动将数字更改为正确的位置并发送包含该关键字的文本,则它可以完美地运行。

不幸的是array_search无法正常工作,因为它无法接受动态指针。有没有办法根据找到的关键字自动填充数组位置?在上面的代码中,您可以看到(希望)我想要做的事情。

1 个答案:

答案 0 :(得分:0)

解决了问题。它可以动态完成。

 $body = $_REQUEST['Body'];

   /* Remove formatting from $body until it is just lowercase
  characters without punctuation or spaces. */

  $result = rtrim($body);
  $result = strtolower($result);
  $text = explode(' ',$result);
  $keyword = array('listing','pigeon_show','owl');
  $word = array_intersect($keyword,$text);
  $key = key($word);

if ($word){
  $word[$key]();}
 else
  {index();}
相关问题