douerqu2319 2016-11-20 13:46
浏览 330

在PHP中的foreach循环期间排除空输入字段

I have a form with 5 sets of inputs. An email and a key. I am creating an array for each set, then looping through them to send an email.

In some cases, not all 5 set of fields will be filled in. In my testing, this still sends a blank email. So I think I need to first create an array of each set of inputs, loop through them and then exclude the array or arrays that's empty?

Here is my current code, any help with an explanation so I can learn would be amazing!

if(isset($_POST['submit'])) {

  $donors = array_map(null, $_POST['email'], $_POST['key']);  

  foreach( $donors as $donor) {
    // Mail script will go here.
  }
}

Would I use another isset, or empty? And how?

  • 写回答

1条回答 默认 最新

  • douchiwan1503 2016-11-20 14:20
    关注

    Figured it out. It was pretty simple once I pointed myself in the right direction.

    $donors = array_map(null, $_POST['email'], $_POST['key']);
    foreach( $donors as $donor) {
      if(!empty($donor[0])&&!empty($donor[1])) {
        // Do something for all those arrays that are not empty
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀