无法检查/验证多维数组

时间:2016-02-29 16:29:14

标签: php arrays multidimensional-array verification


在我搜索并尝试了许多替代方法来检查/验证多维数组后,我放弃了,我决定在这里打开一个新线程(问一个问题)。所以,让我告诉你我到目前为止的进展 我有一个表单,通过_POST,在multidimesional数组中提交信息。在帖子之后,数组显示如下:

Array ( 
[0] => Array ( 
    [Name] => First Name 
    [Birthday] => 01-02-2016 
    [BirthPlace] => PB1 
    [passNr] => Pass1 
    [passFrom] => 15-02-2016 
    [passTo] => 28-08-2016 
    [seatNr] => 16 
    [seatCode] => 6_4 
    [nrPax] => 1 
        ) 
[1] => Array ( 
    [Name] => Second Name 
    [Birthday] => 02-02-2016 
    [BirthPlace] => PB2 
    [passNr] => Pass2 
    [passFrom] => 16-02-2016 
    [passTo] => 29-08-2016 
    [seatNr] => 15 [seatCode] => 6_3 
    [nrPax] => 1 
        ) 
[2] => Array ( 
    [Name] => Third Name 
    [Birthday] => 03-02-2016 
    [BirthPlace] => PB3 
    [passNr] => Pass3 
    [passFrom] => 17-02-2016 
    [passTo] => 30-08-2016 
    [seatNr] => 14 
    [seatCode] => 6_2 
    [nrPax] => 1 
        ) 
[3] => Array ( 
    [Name] => Fourth Name 
    [Birthday] => 04-02-2016 
    [BirthPlace] => PB4 
    [passNr] => Pass4 
    [passFrom] => 18-02-2016 
    [passTo] => 31-08-2016 
    [seatNr] => 13 
    [seatCode] => 6_1 
    [nrPax] => 1 
        ) 
  ) 

我需要做的是验证所有项目是否都是提交/如果其中任何项目为空/空。我试过这个:

foreach ($_POST['traveler'] as $vf) {
if(empty($vf['Name'])) {echo "Missing Name.<br />";}
if(empty($vf['Birthday'])) {echo "Missing Birthday.<br />";}
if(empty($vf['BirthPlace'])) {echo "Missing BirthPlace.<br />";}
if(empty($vf['passNr'])) {echo "Missing passNr.<br />";}
if(empty($vf['passFrom'])) {echo "Missing passFrom.<br />";}
if(empty($vf['passTo'])) {echo "Missing passTo.<br />";}
if(!empty($vf['Name']) && !empty($vf['Birthday']) && !empty($vf['BirthPlace']) && !empty($vf['passNr']) && !empty($vf['passFrom']) && !empty($vf['passTo']) ) { echo "Everything OK. Do the insertion in the mysql";}
 }

问题是,如果多维数组中有3个数组,则msg“Everything OK ...”出现三次;如果是4个数组,则出现4次...因此,如果我执行sql插入,它将被添加为数组的次数。
另外,我应该提到Birthday,passFrom和passTo是日期的输入字段,如果用户将它们留空,不填充它们,则在数组中,这些字段将显示为php default date 01.01.1970;在这种情况下,如何验证用户是否正确填写表格? 在此先感谢,并希望我明确解释这个问题。

1 个答案:

答案 0 :(得分:0)

对于多维数组,您应该在父循环中使用循环 像这样的东西:

$Array = array(
array('f1'=>1,'f2'=>2,'f3'=>3),
array('f4'=>4,'f5'=>5,'f6'=>6),
array('f7'=>7,'f8'=>8,'f9'=>9)
);

foreach($Array as $key => $val){
    foreach($val as $key_2 => $val_2){
        if (empty($val_2)){
            echo $key_2 . 'is empty';
        }
    }
}