Jump to content


Photo

setting array values inside a while loop


  • Please log in to reply
4 replies to this topic

#1 whittierpc

whittierpc
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 22 August 2006 - 12:29 AM

how would I get a value to be set in an array inside a while loop and when it looped the second and third time thos values would be the second and third values of the array?

Many Thank!

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 22 August 2006 - 12:36 AM

$val = array();
while (YOUR CLAUSE)
{
$val[] = $x;
}
follow me on twitter @PHPsycho

#3 Jeremysr

Jeremysr
  • Members
  • PipPipPip
  • Advanced Member
  • 199 posts
  • LocationSaskatchewan, Canada

Posted 22 August 2006 - 12:39 AM

A better way is to do it in a for loop:

for ($x = 0; $x <= 10; $x++) {
     $arrayname[$x] = $x;
}

What this would do is store all the numbers from 0 to 10 in the array, but I'm sure you'll figure it out for whatever you're trying to do.

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 22 August 2006 - 01:02 AM

the for loop requires you know how many variables you want to put in the array...

and the while loop is the simplest most efficient loop in php.

SO IMO the for loop is inferior in this case.
follow me on twitter @PHPsycho

#5 Jeremysr

Jeremysr
  • Members
  • PipPipPip
  • Advanced Member
  • 199 posts
  • LocationSaskatchewan, Canada

Posted 22 August 2006 - 01:24 AM

Ok I was kind of thinking of when you use a for loop to loop through an array printing out each value in the array or something...but I guess he doesn't want to do that.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users