0

Here is the function description

test($argv)

$argv is an array, for example $argv=array($from1,$to1,$from2,$to2.....); array items must be even.

$argv=array(1,3,4,5) : this will output values like below:

1_4
1_5
2_4
2_5
3_4
3_5

pseudocode is like

while($from1<=$to1){
   while($from2<=$to2){
         echo $from1."_".$from2."<br/>";
        $from2++;
   }
   $from1++;
}

$argv=array(1,2,3,4,5,6) : this will output values like below:

1_3_5
1_3_6
1_4_5
1_4_6
2_3_5
2_3_6
2_4_5
2_4_6

pseudocode is like

while($from1<=$to1){
   while($from2<=$to2){
        while($from3<=$to3){
             echo $from1."_".$from2."_".$from3."<br/>";
             $from3++;
        }
        $from2++;
   }
   $from1++;
}

The number of array $argv's is not constant. Maybe 3 or 4 levels of loop will be outputed.

i just heard that tail recuision or iteration will be used, but i don't know exactly how to code.

flag

60% accept rate
Could you generalize what this function is supposed to do? AFAIU it should output every possible combination of $from values and $to values..? – deceze Apr 13 at 4:54
@deceze question has been edited to discript more accurate. – tunpishuang Apr 13 at 5:12
1  
Sorry, that's even less clear. Why is there a 2 output in the first example, there's no 2 in the input? Please just try to put the logic into words, that'll help you as well. – deceze Apr 13 at 5:14
@deceze: +1 for that last sentence. It appears that each 'from','to' pair represents a range of values between 'from' and 'to' (inclusive) – wallacoloo Apr 13 at 5:16
1  
Doing this recursive is way harder then iterative ... why do you need it recursive? – Buttink Apr 13 at 5:19
show 3 more comments

3 Answers

1

The below function should do what you want. It doesn't print the result but instead returns an array with the results that you can iterate over and print it.

$testArray = array('1', '3', '4', '5');
$testArray2 = array('1', '2', '3', '4', '5', '6');

function test($a) {
  $from1 = array_shift($a);
  $to1 = array_shift($a);

  $result = array();

  while ($from1 <= $to1) {
    if (sizeof($a) > 0) {
      $rest = test($a);
      foreach ($rest as $b) {
        $result[] = $from1.'_'.$b;
      }

    } else {
      $result[] = $from1;
    }
    $from1++;
  }
  return $result;
}

print_r(test($testArray));
print_r(test($testArray2));

As an advice read up on recursion as it's an extremely useful technique. It's sometimes hard to wrap your head around it but it's well worth the effort. This book from MIT has a great chapter on recursion if i remember correctly, and its free. Have fun.

link|flag
This answer is totaly wrong. LOL – jondro Apr 13 at 6:32
fixed it to do the proper thing (I hope). – jondro Apr 13 at 6:43
@jondro thx, u solved it. – tunpishuang Apr 13 at 7:29
1

Here are some functions:

    header("Content-type: text/plain");

    function testFromTo($args) {
        echo "***** testFromTo *****\n";
        // uses indexes parity to walk the array
        foreach ($args as $k1=>$v1) {
            if (($k1 + 1) % 2 == 1) {
                foreach ($args as $k2=>$v2) {
                    if (($k2 + 1) % 2 == 0) {
                        echo $v1 . "_" . $v2 . "\n";
                    }
                }
            }
        }
    }

    function testHalf($args) {
        echo "***** testHalf *****\n";
        // cuts $args in two parts
        $args1 = array_slice($args, 0, count($args) / 2);
        $args2 = array_slice($args, count($args) / 2);

        foreach ($args1 as $v1) {
            foreach ($args2 as $v2) {
                echo $v1 . "_" . $v2 . "\n";
            }
        }
    }


    $argv = array(1, 2, 3, 4, 5, 6);
    testFromTo($argv);
    testHalf($argv);

    exit;
?>

and associated outputs:

***** testFromTo *****
1_2
1_4
1_6
3_2
3_4
3_6
5_2
5_4
5_6
***** testHalf *****
1_4
1_5
1_6
2_4
2_5
2_6
3_4
3_5
3_6
link|flag
No more OK with edited question.. – RC Apr 13 at 5:23
0

Something like this?

test("1", "2", "4", "5", "6", "7", "8", "9");

function test(){
    $args = func_get_args();
    $cnt = func_num_args();
    //echo "cnt :: " . ($cnt % 2) . "\n";

    if(($cnt % 2) > 0) return false;

    $split = $cnt / 2;
    list($list1, $list2) = array_chunk($args, ($cnt / 2));
foreach($list1 as $idx=>$val){
    foreach($list2 as $idx2=>$val2){
        echo $list1[$idx] . "_" . $list2[$idx] . "\n";
    }
}       
}

Here's the output:

1_6
1_6
1_6
1_6
2_7
2_7
2_7
2_7
4_8
4_8
4_8
4_8
5_9
5_9
5_9
5_9
link|flag
@silent no,not like this,dude. recursive must be used to output what i need. – tunpishuang Apr 13 at 5:06
@tunpishuang Why do you insist on recursion? The best you could do here is tail recursion, which is the same as a loop. – deceze Apr 13 at 5:09
oops, didn't see the updated question – silent Apr 13 at 5:31
2  
I think this is from school task. – silent Apr 13 at 6:34
@slient yeah,that's right,i am a newbie in programming php. – tunpishuang Apr 13 at 7:22

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.