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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
edmund
Forum Newbie
Posts: 1
Joined: Sat May 04, 2002 10:12 am

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

Post 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
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post 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[#]
Post Reply