Results 1 to 9 of 9
Thread: PHP - For loop multi-dimensional
-
01-10-2009, 02:47 PM #1Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
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.
-
01-10-2009, 03:01 PM #2Web Hosting Master
- Join Date
- Jan 2006
- Location
- Athens, Greece
- Posts
- 1,481
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;
-
01-10-2009, 03:09 PM #3Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
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 ?
-
01-10-2009, 03:52 PM #4Web Hosting Master
- Join Date
- Jan 2006
- Location
- Athens, Greece
- Posts
- 1,481
2 arrays is much easier, don't tell me u can not mod it to take 2 arrays!!?
-
01-11-2009, 12:28 PM #5Web Hosting Master
- Join Date
- Aug 2005
- Location
- UK
- Posts
- 654
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.
-
01-11-2009, 12:32 PM #6Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
I found the solution on my own.
-
01-11-2009, 03:49 PM #7Dennis Johnson
- Join Date
- Jun 2001
- Location
- Kalamazoo
- Posts
- 33,412
I found the solution on my own.What was that? Care to share?
-
01-11-2009, 03:51 PM #8Dennis Johnson
- Join Date
- Jun 2001
- Location
- Kalamazoo
- Posts
- 33,412
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?
-
01-11-2009, 03:57 PM #9Web Hosting Master
- Join Date
- Jan 2006
- Location
- Athens, Greece
- Posts
- 1,481
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.