dtoq41429 2010-02-22 21:06
浏览 37

比较两个数组值

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.

  • 写回答

1条回答 默认 最新

  • doushi3322 2010-02-22 21:17
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条