What is wrong in my statement; getting undefined bias?

I keep getting the offset undefined .. What would be the problem with this?

for($m=0; $m<=count($data); $m++){ // (this is where it points)
  if(date("m-Y",strtotime($data['data'][$i]['date_d'])) == $curdate)
    $sum.$cmonth +=1;
  else
    $sum.$cmonth = 0;
}
+4
source share
2 answers

This is because adding <=will make your loop iterate over to a nonexistent array index. Rewrite it like this.

for($i=0; $i<count($data); $i++){ // (this is where it points)
  if(date("m-Y",strtotime($data['data'][$i]['date_d'])) == $curdate)
    $sum.$cmonth +=1;
  else
    $sum.$cmonth = 0;
}

I recommend it instead foreach.

foreach($data as $k=>$v)
{

}
+2
source

I see that your array is multidimensional and you are starting a loop at the second index, so for example

for($m=0; $m<count($data['data']); $m++){

This will consider the length of the array (if any) with the name datainside the variable $data.

And change your $ i to $ m (if you are trying to traverse this array in a loop) like

if(date("m-Y",strtotime($data['data'][$m]['date_d'])) == $curdate)
0
source

Source: https://habr.com/ru/post/1534190/


All Articles