1

I'm trying to get some data from server with the help of php script, that looks like:

$count_list = array();
while($row = $result->fetch_assoc()){
    $count_list[$row['index']][$row['id']] = $row['count'];
}
$result->close();

for($i = 1; $i < 5; $i++){
    if($count_list[$i] === null || $count_list[$i][CONTENT_ID1] === null){
        $count_list[$i][CONTENT_ID1] = 0;
    }
    if($count_list[$i] === null || $count_list[$i][CONTENT_ID2] === null){
        $count_list[$i][CONTENT_ID2] = 0;
    }
    if($count_list[$i] === null || $count_list[$i][CONTENT_ID3] === null){
        $count_list[$i][CONTENT_ID3] = 0;
    }

    $_smarty->assign("p".$i."c", min($count_list[$i][CONTENT_ID1], $count_list[$i][CONTENT_ID2], $count_list[$i][CONTENT_ID3]));
}

I'm getting "undefined offset", "undefined index" Notices. As you can see, I even added some checks inside for-loop to ensure, that there will be no null values for min() function, but then the same Notices are showing right at those check lines. What am I doing wrong, and how should I get rid of those Notices. I just want, every non-existing element of my array to be 0. thanks in advance.

Aki
  • 11
  • 2
  • Keep CONTENT_ID1,CONTENT_ID2,CONTENT_ID3 inside single quote as 'CONTENT_ID1' and so on for other and see if that works. – Gyan Jul 12 '17 at 05:12
  • There are probably fewer than 5 items in your `$count_list` array yet your `for` loop expects at least 5 – Phil Jul 12 '17 at 05:15
  • OK, apparently not all arrays in php are treated as associative. I was thinking that, if one can add some element to empty array in the way $a['a']='b';, there shouldn't be problems to add one more dimension into this. I was wrong. I ended with initializing my array with zero values – Aki Jul 12 '17 at 07:00

0 Answers0