txrandom Posted August 8, 2006 Share Posted August 8, 2006 In my code, I have an inputted string exploded into an array. I can type echo "$invitearray[0] $invitearray[1] ... "; and get it to display certain elements within an array. The only problem is I'm trying to run a for loop, and to do that I need the total number of elements in the array. When I run $sizearray = count($invitearray);, $sizearray equals 0. Any idea what's wrong?[code]$invitearray = explode(',', $invitelist);echo "$invitearray[0] $invitearray[1] $invitearray[2] $invitearray[3]";$sizearray = count($invitearrary);echo "<br><br>$sizearray";for ($j=0; $j<$sizearray; $j++) {echo "$invitearray[$j]";}[/code] Link to comment https://forums.phpfreaks.com/topic/16967-having-problems-with-count/ Share on other sites More sharing options...
Barand Posted August 8, 2006 Share Posted August 8, 2006 [code]<?php$invitearray = explode(',', $invitelist);foreach ($invitearray as $val) { echo $val, '<br />';}?>[/code] Link to comment https://forums.phpfreaks.com/topic/16967-having-problems-with-count/#findComment-71472 Share on other sites More sharing options...
SharkBait Posted August 8, 2006 Share Posted August 8, 2006 What about a foreach?[code]<?phpforeach($invitearray as $loc => $value) { echo "Loc {$loc} - {$value}<br />";}?>[/code]But for a ForLoop[code]<?php$sizearray = count($invitearray);for($j = 0; $J <= $sizearray -1; $j++) { echo "{$invitearray[$j]} <br />";}?>[/code]Notice that in the for look I subtract 1 from the count since arrays are base 0 Link to comment https://forums.phpfreaks.com/topic/16967-having-problems-with-count/#findComment-71474 Share on other sites More sharing options...
king arthur Posted August 9, 2006 Share Posted August 9, 2006 Well, unless this is a typo and not your actual code:[code]$sizearray = count($invitearrary);[/code]you are getting the size of an array which is not defined, hence the result of zero! Link to comment https://forums.phpfreaks.com/topic/16967-having-problems-with-count/#findComment-71512 Share on other sites More sharing options...
bltesar Posted August 9, 2006 Share Posted August 9, 2006 you have $invitearrary instead of $invitearray on the line $sizearray = count($invitearrary); Link to comment https://forums.phpfreaks.com/topic/16967-having-problems-with-count/#findComment-71515 Share on other sites More sharing options...
txrandom Posted August 9, 2006 Author Share Posted August 9, 2006 Thanks everyone. I swore I checked it for grammar and didn't find anything. Just a stupid mistake! Link to comment https://forums.phpfreaks.com/topic/16967-having-problems-with-count/#findComment-71599 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.