Jump to content


Photo

Having problems with count()


  • Please log in to reply
5 replies to this topic

#1 txrandom

txrandom
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 08 August 2006 - 10:46 PM

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?

$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]";


}


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 08 August 2006 - 10:50 PM

<?php
$invitearray = explode(',', $invitelist);
foreach ($invitearray as $val) {
         echo $val, '<br />';
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 08 August 2006 - 10:51 PM

What about a foreach?

<?php

foreach($invitearray as $loc => $value) {
   echo "Loc {$loc} - {$value}<br />";
}
?>

But for a ForLoop
<?php

$sizearray = count($invitearray);

for($j = 0; $J <= $sizearray -1; $j++) {
  echo "{$invitearray[$j]} <br />";
}
?>

Notice that in the for look I subtract 1 from the count since arrays are base 0


#4 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 09 August 2006 - 12:13 AM

Well, unless this is a typo and not your actual code:
$sizearray = count($invitearrary);

you are getting the size of an array which is not defined, hence the result of zero!
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#5 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 09 August 2006 - 12:19 AM

you have $invitearrary instead of $invitearray on the line

    $sizearray = count($invitearrary);



#6 txrandom

txrandom
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 August 2006 - 03:03 AM

Thanks everyone.  I swore I checked it for grammar and didn't find anything.  Just a stupid mistake!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users