Why gives the undefinded index error?

98 Views Asked by At
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];
.
.
.
.
}
}
}

It's my code and when I use it :

> 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

I use isset() to check my data. if all is set why produces an error?

in this code we have 2 $_POST and they are checked in line 1 and other data is array and I check it 100 time and I do not see where is error. Please see lines 8 and 9. They do not have error. Although the structure is similar to other lines.Why?

1

There are 1 best solutions below

2
On

When u do isset($r['t']['b']['v']);

It assumes $r['t']['b'] already exists, and checks only the ['v'].
If those ['t'] or ['b'] are none existent PHP will emit a warning, even if it is inside isset

So to check such multiple dim array is actually:

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