Results 1 to 9 of 9
  1. #1
    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.

  2. #2
    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;

  3. #3
    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 ?

  4. #4
    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!!?

  5. #5
    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.

  6. #6
    Join Date
    Mar 2006
    Posts
    984
    I found the solution on my own.

  7. #7
    Join Date
    Jun 2001
    Location
    Kalamazoo
    Posts
    33,412
    I found the solution on my own.What was that? Care to share?

  8. #8
    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?

  9. #9
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •