Re: Dynamic Array Index using Variable
| From: | Al | Date: | Tue, 10 Jan 2006 02:29:46 +0000 |
| Subject: | Re: Dynamic Array Index using Variable | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-228384@lists.php.net to get a copy of this message | ||
Chris Lott wrote:
What I have is a form with 45 data fields... if any of a selected 35 of those fields is empty, I want to have the user go back. So I can't loop over the entire _POST array. So I thought I would put the names of the fields I *do* want to check into an array and check. So this won't work:Simple... Make an array of the field names to test, $check_fields= array('field1', ....). Then foreach($check_fields as $field){ if(empty($_POST[$field])) $err_array[]= $field; } $err_array is a list of the empty fields.foreach($array as $key=>$value){ if(empty($value)){Because the array is the names of the fields, and I want to check each variable in _POST that matches. So instead of writing if (empty($_POST['forma']) if (empty($_POST['formb']) etc I would do something like $fieldstocheck = array('forma', 'formb'); foreach ($fields as $thisfield) { if (empty($_POST[$thisfield])) { set the error flag; } But that doesn't seem to work. Eventually I will use the array to associate error text: $fieldstocheck = array('forma' => 'You must enter field A', 'formb' => ''Field B is your age in years and is required'); etc c$err_msg= "$key is empty"; break;