PHP - 我对$ _POST和in_array有点困惑

时间:2016-04-13 12:47:28

标签: php

好吧,所以我从$ _POST收到,我需要我的代码做这样的事情

if ($_POST contains EU-London) {
//do stuff here
}else{
//failed to find EU-London
}

现在我被告知在代码中找到某个短语的几种方法,但发布的数据包含:

 Array
 (
     [EU-London] => 
 )

我如何检查欧盟 - 伦敦是否在那里?因为pregmatch使用字符串,我不知道如何使用in_array()

来获取它

2 个答案:

答案 0 :(得分:4)

您似乎在$_POST中寻找

if (isset($_POST['EU-London'])) {
  // Key exists.
}

正如罗伯特正确评论的那样,检查现有密钥的正确方法是

if (array_key_exists('EU-London', $_POST)) {
  // Key exists.
}

答案 1 :(得分:2)

您可以检查是否设置了密钥

if (isset($_POST['EU-London'])) {
    //key isset
}