benphp Posted September 23, 2008 Share Posted September 23, 2008 I have something that looks like this: <?php for ($i=0;$i<=$rowcount;$i++) { $sum = $rows[$i]; } ?> But the $rowcount isn't always the same, therefore I get the Undefined offset error. Is there a function that can determine if the offset will be undefined before it attempts the loop? Otherwise, I'll need to write more lines to an already large page. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/125546-array-help-detect-undefined-offset/ Share on other sites More sharing options...
kenrbnsn Posted September 23, 2008 Share Posted September 23, 2008 do <?php $sum = 0; $rowcount = count($rows); for ($i=0;$i<$rowcount;$i++) $sum += $rows[$i]; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/125546-array-help-detect-undefined-offset/#findComment-649118 Share on other sites More sharing options...
CroNiX Posted September 24, 2008 Share Posted September 24, 2008 As kenrbnsn pointed out, you need to use < not <=. The reason is count() will start counting at 1 but arrays start at 0, so it will never be equal to which is why you get the error. It is looking for an index 1 greater than the actual size of the array when you have the =. Quote Link to comment https://forums.phpfreaks.com/topic/125546-array-help-detect-undefined-offset/#findComment-649191 Share on other sites More sharing options...
benphp Posted September 24, 2008 Author Share Posted September 24, 2008 The issue is a bit more complicated - I simplified it for the question, but here is the actual code. Maybe one of you wizards can figure this one out. The trick is that $colcount may be greater than the elements in the $rows array, because the elements in the $rows array varies: <?php for ($x=0;$x<=$colcount-1;$x++) { print "<td>"; $sum = 0; for ($i=0;$i<=$rowcount-1;$i++) { //cycle through the row array //$rows [row] [column] $sum = $sum + $rows[$i][$x]; $avg = round($sum / $rowcount, 2); } print $avg; print "</td>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/125546-array-help-detect-undefined-offset/#findComment-649466 Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2008 Share Posted September 24, 2008 Use a foreach() loop, it iterates over each element in an array and it does not care what the indexes are or how many of them there are. Quote Link to comment https://forums.phpfreaks.com/topic/125546-array-help-detect-undefined-offset/#findComment-649470 Share on other sites More sharing options...
discomatt Posted September 24, 2008 Share Posted September 24, 2008 Alternately, use an isset() call. for ($i=0;$i<=$rowcount;$i++) if ( isset($rows[$i]) ) $sum = $rows[$i]; Quote Link to comment https://forums.phpfreaks.com/topic/125546-array-help-detect-undefined-offset/#findComment-649481 Share on other sites More sharing options...
benphp Posted September 24, 2008 Author Share Posted September 24, 2008 The isset works! When I try a foreach on $rows, it doesn't work as I expect - probably because I don't understand the multidimensional array and how it works. foreach ($rows as $temp) { provides lots of "Illegal offset type" errors. Quote Link to comment https://forums.phpfreaks.com/topic/125546-array-help-detect-undefined-offset/#findComment-649494 Share on other sites More sharing options...
sasa Posted September 24, 2008 Share Posted September 24, 2008 can you print_r($rows); and post output Quote Link to comment https://forums.phpfreaks.com/topic/125546-array-help-detect-undefined-offset/#findComment-649596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.