简体   繁体   中英

Comparing two array values

I have a form used to void, dismiss, amend and correct citations. A void can not be done in conjucntion with a dismissal, amendment, or correction. If you need to void and amend, you complete two forms. You can however do a dismissal, amendment and/or correction on the same form. I have two arrays created based on the variables from the form. One array has the variables from the void section, the other array has all the other variables.

I am trying to create a rule that compares the void to all others. I want to be able to check if any void variables are != "" and any of the other variables are != "" and then give an error message.

if($_POST[$all_other_fields] !== "" && $_POST[$void_fields] !== "")
                {
                  $all_ok = false;
        $citation_error_msg = "Blah, Blah Blah";
                 }

What I have done, unwittingly, is stated ALL varriables from both arrays must != "". I want to say if even one variable from both arrays != "", then error message.

I am not sure how to make the comparison.

So, you're saying that if a void field is filled out, throw an error if someone fills out other sections? I might restructure my form into a paged form, where you ask what the user wishes to do and then present them with a specific form to do it. Having extraneous fields on a form just makes things confusing for the user.

In any event, if you stick with your current form design, you'll need to have an array of void field names against which you'll check your $_POST. Something like:

$vf_flag=false;
$void_fields=array('vf1','vf2','vf3');
    foreach($void_fields as $vf) {
      if(!isset($_POST[$vf])) continue;
      $vf_flag=true;
      break; //only need one error
    }
$nonvf_flag=false;
//do the same as above except with a non_voidfields array()

if($nonvf_flag && $vf_flag) $citation_msg='blah blah blah';

This is a verbose way, but it's straightforward. You could also do it with array_filter() and a callback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM