php - I'm receiving Undefined offset error -
why receive undefined offset error
? i'm trying add 10,20,20
each element in array. please help. in advance
<?php $arr = array("a","b","c"); $counter = 0; $status = array(); foreach($arr $a){ $status[$counter] += 10; $status[$counter] += 20; $status[$counter] += 20; echo $status[$counter]."<br>"; $counter ++; } ?>
error:
notice: undefined offset: 0 in c:\xampp\htdocs\test\index.php on line 6 300 notice: undefined offset: 1 in c:\xampp\htdocs\test\index.php on line 6 300 notice: undefined offset: 2 in c:\xampp\htdocs\test\index.php on line 6 300
`
in code, $status
empty array, when attempt add undefined index see notice (only first time).
to initialize $status
array values 0 based on number of elements in $arr
:
$status = array_fill(0, count($arr), 0);
Comments
Post a Comment