为什么给出未定义的索引错误?

时间:2013-09-08 02:06:57

标签: php undefined-index

if(isset($_POST['dino_c']) AND isset($_POST['dino_box']) AND isset($dino_recruit[$a_dino[$_POST['dino_c']][1]][$a_dino[$_POST['dino_c']][3]][$_POST['dino_box']])){
if(....){
if(($pop>(0-1+$dino_recruit[$a_dino[$_POST['dino_c']][1]][$a_dino[$_POST['dino_c']][3]][$_POST['dino_box']][4])) AND ($resource[0]>(0-1+$dino_recruit[$a_dino[$_POST['dino_c']][1]][$a_dino[$_POST['dino_c']][3]][$a_dino[$_POST['dino_box']]][5] -1)) AND ($resource[1]>(0-1+$dino_recruit[$a_dino[$_POST['dino_c']][1]][$a_dino[$_POST['dino_c']][3]][$a_dino[$_POST['dino_box']]][6] -1)) AND ($resource[2]>(0-1+$dino_recruit[$a_dino[$_POST['dino_c']][1]][$a_dino[$_POST['dino_c']][3]][$a_dino[$_POST['dino_box']]][7] -1))){
.
.
.
.
$pop = $pop - $dino_recruit[$a_dino[$_POST['dino_c']][1]][$a_dino[$_POST['dino_c']][3]][$_POST['dino_box']][4];
$pop_consum = $pop_consum + $dino_recruit[$a_dino[$_POST['dino_c']][1]][$a_dino[$_POST['dino_c']][3]][$_POST['dino_box']][4];
$resource[0] = 0 + $dino_recruit[$a_dino[$_POST['dino_c']][1]][$a_dino[$_POST['dino_c']][3]][$a_dino[$_POST['dino_box']]][5];
$resource[1] = 0 + $dino_recruit[$a_dino[$_POST['dino_c']][1]][$a_dino[$_POST['dino_c']][3]][$a_dino[$_POST['dino_box']]][6];
$resource[2] = 0 + $dino_recruit[$a_dino[$_POST['dino_c']][1]][$a_dino[$_POST['dino_c']][3]][$a_dino[$_POST['dino_box']]][7];
$dino_add[$a_dino[$_POST['dino_c']][1]-1] = 0 + $dino_recruit[$a_dino[$_POST['dino_c']][1]][$a_dino[$_POST['dino_c']][3]][$a_dino[$_POST['dino_box']]][0];
.
.
.
.
}
}
}

这是我的代码,当我使用它时:

> undefined index:4 in .... ajax.php on line 672  => here line 3
> undefined index: in .... ajax.php on line 672  => here line 3
> undefined index:4 in .... ajax.php on line 672  => here line 3
> undefined index: in .... ajax.php on line 672  => here line 3
> undefined index:4 in .... ajax.php on line 672  => here line 3
> undefined index: in .... ajax.php on line 672  => here line 3
> undefined index:4 in .... ajax.php on line 679  => here line 10
> undefined index: in .... ajax.php on line 679  => here line 10
....
> undefined index:4 in .... ajax.php on line 682  => here line 13
> undefined index: in .... ajax.php on line 682  => here line 13

我使用isset()来检查我的数据。如果全部设置为什么会产生错误?

在这段代码中,我们有2 $ _POST,它们在第1行检查,其他数据是数组,我检查了100次,我没有看到错误在哪里。 请参阅第8和第9行。它们没有错误。虽然结构类似于其他线。为什么?

1 个答案:

答案 0 :(得分:4)

当你做isset($r['t']['b']['v']);

它假设$r['t']['b']已存在,并仅检查['v'] 如果那些['t']['b']不存在,PHP将发出警告,即使它在isset

所以要检查这样的多个dim数组实际上是:

if(isset($r['t'])&&isset($r['t']['b'])&&isset($r['t']['b']['v'])){...