How can I make a variable into a key or a value so that I can use it in in_array()
?
For instance,
// set the variable
$cnt_firstname = trim($_POST['cnt_firstname']);
$required = trim($_POST['required']);
// set the array for required fields
$array_required = explode(' ',$required);
// check the item is not empty and if it is in the array of required
if(empty($cnt_firstname) && in_array($cnt_firstname, $array_required))
{
$error = true;
echo '<error elementid="cnt_firstname" message="Please enter your fist name." />';
}
this is the post data I would send from a form,
Array
(
[cnt_firstname] =>
[cnt_lastname] =>
[cnt_organisation] =>
[cnt_email1] =>
[cnt_telephpne] =>
[required] => cnt_firstname cnt_lastname cnt_email1
)
I cannot get the result. I want it to return false if the field is empty and that field is in required array.
How can I make it work?
Quick & Dirty:
if(empty($cnt_firstname) && in_array('cnt_firstname', $array_required))
The way you wrote it won't work, because it will only check, if $cnt_firstname is in the required array, if it's empty - in other words: is an empty string in array?