Check if an array is in the key of another array in PHP
I've an array like $ignore_post = array("foo", "开发者_开发技巧bar");
and I need to check if foo
or bar
is a key for $_POST
(if $_POST["foo"]
or $_POST["bar"]
exists).
How I can do that?
Thank you in advance
You can use PHP function array_key_exists
:
<?php
foreach($ignore_post as $key)
{
if(array_key_exists($key,$_POST))
{
// ...
}
}
?>
Alternatively you can replace array_key_exists($key,$_POST)
with isset($_POST[$key])
Please try using array_key_exists php function.
For reference, visit array_key_exists
You can do it like this
<?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'
}
}?>
精彩评论