Jump to content

Having problems with count()


txrandom

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]
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.