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
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])
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'
}
}?>