0

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

3
  • are you familiar with a thing called "loop"? Commented Sep 21, 2011 at 9:08
  • @Col.Shrapnel: yes but I wanted to know if it was a way of doing without a loop :-) Commented Sep 21, 2011 at 9:09
  • there is noway to deal with arrays without loops. EVEN if you don't see it, a loop is always involved. go figure Commented Sep 21, 2011 at 9:11

3 Answers 3

2

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])

Sign up to request clarification or add additional context in comments.

Comments

0

Please try using array_key_exists php function.

For reference, visit array_key_exists

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.