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
https://forums.phpfreaks.com/topic/16967-having-problems-with-count/
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

Archived

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

×
×
  • 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.