Page 1 of 1

How to make the result of a "for" loop to be array

Posted: Sat May 04, 2002 10:12 am
by edmund
If I want to operate an array, e.g. 1,2,3,4,5..... and multiply by 2 on each of them by a for loop to become 6,7,8,9,10......

How can I make them to remain as an array rather then just printing them out? All tutorials just simply introducing the for loop as printing them out :cry: .

THANKS! :P

Posted: Sat May 04, 2002 11:02 am
by dusty
don't echo them out until you need to..

Code: Select all

<?
$array = array('1','2','3','4','5');

for($i=0;$i<count($array);$i++) &#123;
  $array&#1111;$i] = $array&#1111;$i]+5;
&#125;

foreach($array as $num) &#123;
  echo $num . "<br>";
&#125;
?>
btw, multiplying by 2 won't make 6,7,8,9,10. +5 will

if you want to echo each at a time, just take out the foreach and use $array[#]