Jump to content

Archived

This topic is now archived and is closed to further replies.

txrandom

Having problems with count()

Recommended Posts

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]

Share this post


Link to post
Share on other sites
[code]<?php
$invitearray = explode(',', $invitelist);
foreach ($invitearray as $val) {
        echo $val, '<br />';
}
?>[/code]

Share this post


Link to post
Share on other sites
What about a foreach?

[code]
<?php

foreach($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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
you have $invitearrary instead of $invitearray on the line

    $sizearray = count($invitearrary);

Share this post


Link to post
Share on other sites
Thanks everyone.  I swore I checked it for grammar and didn't find anything.  Just a stupid mistake!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.