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] Quote Link to comment 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] Quote Link to comment 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 Quote Link to comment 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! Quote Link to comment 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); Quote Link to comment 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! Quote Link to comment 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.