Page 1 of 1

Repeat a command multiple times???

Posted: Thu Jun 10, 2004 12:55 pm
by Chris Corbyn
Hi,

does anyone know a way to do something X amount of times in sequence, for example, if I have an array of numbers and I want to print a star, *, X amount of times where X is the number.

Code: Select all

$array = array(4, 7, 9, 10, 56, 78, 99);

echo '*' * $array[0]; /*Not the way to do it but just to indicate what I'm trying to do*/
So in the case above a star is printed times. What's the correct way to do this?

Thanks

Posted: Thu Jun 10, 2004 1:23 pm
by magicrobotmonkey

Code: Select all

<?php
foreach($array as $value){
    for($i=0;$i<$value;$i++) {echo '*';}
}
?>

Posted: Thu Jun 10, 2004 2:17 pm
by feyd

Re: Repeat a command multiple times???

Posted: Thu Jun 10, 2004 5:26 pm
by d3ad1ysp0rk

Code: Select all

$array = array(4, 7, 9, 10, 56, 78, 99);
$x = $array[0];
for($i=0;$i<$x;$i++){
echo "*";
}

Posted: Fri Jun 11, 2004 4:11 am
by Sycor
To loop through each element in the array you could always use this...

Code: Select all

<?php

$array = array(4, 7, 9, 10, 56, 78, 99);

for ($element = 0; $element < sizeof($array); $element++)
{
for ($count = 0; $count < $array[$element]; $count++)
{
echo "*";
}
}

?>

Posted: Fri Jun 11, 2004 8:42 am
by magicrobotmonkey
uhh that's what mine did but ill change it to:

Code: Select all

<?php
foreach($array as $value){ 
  str_repeat("*",$value);
} 
?>

?>

Posted: Sat Jun 12, 2004 12:08 pm
by Chris Corbyn
Thanks everyone :-D

Sorry I abandoned this topic, got busy working on something else :-/