note 95664 deleted from function.array-key-exists by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 20:41:12 +0000 |
| Subject: | note 95664 deleted from function.array-key-exists by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218755@lists.php.net to get a copy of this message | ||
Note Submitter: gmdebby at gmail dot com
----
A little function which take an array as keys
<?php
//note the s in the function name (keys)
function array_keys_exists($array,$keys) {
foreach($keys as $k) {
if(!isset($array[$k])) {
return false;
}
}
return true;
}
?>
//useful to validate a form for example
<form>
<input type="text" name="field1" /><br />
<input type="text" name="field2" /><br />
<input type="text" name="field3" /><br />
<input type="text" name="field4" /><br />
<input type="text" name="field5" /><br />
</form>
<?php
if(!array_keys_exists($_POST,
array("field1","field2","field3","field4","field5")
)) {
//some fields are missing, dont do anything (maybe hacking)
} else {
//code ...
}
?>