检查数组是否在PHP中另一个数组的键中

时间:2011-09-21 09:05:01

标签: php

我有一个类似$ignore_post = array("foo", "bar");的数组,我需要检查foobar$_POST的关键(如果$_POST["foo"]或{{ 1}}存在)。

我怎么能这样做?

提前谢谢

3 个答案:

答案 0 :(得分:2)

您可以使用PHP函数array_key_exists

<?php
foreach($ignore_post as $key)
{
    if(array_key_exists($key,$_POST))
    {
        // ...
    }
}
?>

或者,您可以将array_key_exists($key,$_POST)替换为isset($_POST[$key])

答案 1 :(得分:0)

你可以这样做

<?php foreach ($ignore_post as $value){
if(!empty($_POST[$value])){
   echo 'It exists';
} else {
   echo 'It does not exist or is empty'
}
}?>

<?php foreach ($ignore_post as $value){
if(isset($_POST[$value])){
   echo 'It exists but might be empty';
} else {
   echo 'It does not exist'
}
}?>

答案 2 :(得分:0)

请尝试使用array_key_exists php函数。

供参考,请访问array_key_exists