开发者

PHP - recursive problem [duplicate]

开发者 https://www.devze.com 2023-04-06 01:54 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: PHP 2D Array output all combinations
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

PHP 2D Array output all combinations

I need to test the behavior of a certain function with a significant number of possible inputs. Say the function signatures are as follows:

foo ($a)
foo ($a, $b, $c)

Say $a can have the following values: 1, 2.

Say $b can have the following values: 'hello', 'world'.

Say $c can have the following values: TRUE, FALSE

How to write a function that returns the following combinations开发者_StackOverflow中文版:

1
2
1,hello,TRUE
1,hello,FALSE
2,hello,TRUE
2,hello,FALSE
1,world,TRUE
1,world,FALSE
...

Note that the number of function's parameters are unknown and so are their possible values.


This question doesn't seem to have anything to do with recursion.

From what you've written, it seems that you want to test the function foo() with that list of arguments, generated from the possible permutations of each? The following code will generate that list.

//Generate the list
$arg_list = array();
foreach(array(1,2) as $a) //Build the foo($a) cases
   $arg_list[] = array($a); 
foreach(array(1,2) as $a) //Build the foo($a, $b, $c) cases
   foreach(array('hello','world') as $b)
       foreach(array(true,false) as $c)
           $arg_list[] = array($a,$b,$c);

//Test each possible case
foreach($arg_list as $args) {
   ...
   $result = call_user_func_array('foo', $args);
   ...
   //Is result what was expected? Check and aggregate
}

Is this the sort of thing you're after?

0

精彩评论

暂无评论...
验证码 换一张
取 消