Search for all Array values in another Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Search for all Array values in another Array

Post by anjanesh »

I want to make sure $_POST['a'], $_POST['b'] and $_POST['c'] exist.
I need a function to return 3. I thought this would but doesnt :

Code: Select all

echo array_search(array('a','b','c'),array_keys($_POST));
in_array(array('a','b','c'),array_keys($_POST)) is always returning false even when $_POST['a'], $_POST['b'] and $_POST['c'] exist.
Any other way ?
Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

There'll be a better way but here's my take on it :-D

Code: Select all

$reqd = array('a', 'b', 'c');
$check = 0;
foreach ($reqd as $v) {
    if (array_key_exists($v, $_POST)) {
        $check++;
    }
}
if ($check == count($reqd)) {
    //All OK
}
Post Reply