horizon
01-10-2009, 02:47 PM
Hi,
I'm looking for to create two for loops in PHP which must contain the following statement:
Loop 1: 123123123 ... and so on (NOT an unlimited loop please).
Loop 2: 111222333 ... and so on (NOT an unlimited loop please).
Both must be used at the same time in the same loop.
Ex:
for ($i = 0; $i <= 10; $i++) {
for ($j = 0; $j <= 10; $j++) {
}
}
Waiting for your inputs. ;)
Steve_Arm
01-10-2009, 03:01 PM
Err... the quick one
$i = 0;
$j = 0;
$data = array(1,2,3);
$str_a = '';
$str_b = '';
$end = 0;
for ($i = 0; $i <= 10; $i++)
{
$str_a .= $data[0].$data[1].$data[2];
for ($j = 0; $j < 3; $j++)
{
$str_b .= $data[$end];
}
$end++;
if ($end == count($data)) $end = 0;
}
echo $str_a.'<br />'.$str_b;
horizon
01-10-2009, 03:09 PM
Your instruction specifies with one array. My fault, I forgot to state on the first post that it would need to be done from two different arrays. Any ideas ? :)
Steve_Arm
01-10-2009, 03:52 PM
2 arrays is much easier, don't tell me u can not mod it to take 2 arrays!!?
Xeentech
01-11-2009, 12:28 PM
I take it this is your home work?
Maybe you should pay more attention in class if you can't make Steve's example do what you need.
horizon
01-11-2009, 12:32 PM
I found the solution on my own.
SoftWareRevue
01-11-2009, 03:49 PM
I found the solution on my own.What was that? Care to share? :)
SoftWareRevue
01-11-2009, 03:51 PM
2 arrays is much easier, don't tell me u can not mod it to take 2 arrays!!?You mean mod your code for 2 arrays?
Steve_Arm
01-11-2009, 03:57 PM
Yes, I haven't tried it, but the second array for the inner loop ($J)
could be $data_b = array('111','222','333');
Although it looks like a weird solution. Since the first array
can be used to solve the problem.