首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在PHP中为多变量测试创建配方?

在PHP中为多变量测试创建配方?
EN

Stack Overflow用户
提问于 2012-01-06 19:44:37
回答 2查看 54关注 0票数 0

我在创建多变量测试的食谱时遇到了麻烦。例如,如果我想测试一套衣服的组合,我有3种不同的帽子、衬衫和裤子。我想列出它们的所有可能的组合,而不是重复的。到目前为止,这是我的思考过程:

代码语言:javascript
运行
复制
// outfit #1
$outfit[0][0] = "hat A ";
$outfit[0][1] = "shirt A ";
$outfit[0][2] = "pants A ";

// outfit #2
$outfit[0][0] = "hat B ";
$outfit[0][1] = "shirt B ";
$outfit[0][2] = "pants B ";

// outfit #3
$outfit[0][0] = "hat C ";
$outfit[0][1] = "shirt C ";
$outfit[0][2] = "pants C ";

function recipeMaker()
{
    $i = 0;
    $j = 0;
    foreach ($outfit as $outfit_2)
    {
          foreach ($outfit_2 as $outfit_3)
          {
              ...some magic here...
              recipe[$i][$j] = ...something goes here...
              $j++;
          }
     $i++;
    }    
} 

foreach ($recipe as $r)
{
    echo $r . "<br />";
}

然后,它应该输出:

代码语言:javascript
运行
复制
hat A shirt A pants A
hat B shirt A pants A
hat C shirt A pants A
hat A shirt B pants A
etc.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-06 21:29:19

你可以沿着嵌套foreach循环的路线走下去,但是当你想要扩展装备(例如添加领带列表)时会发生什么呢?下面是一个解决方案,它可以输出来自任意数量集合的组合:

代码语言:javascript
运行
复制
class Combiner {
    protected $_collections;
    protected $_combinations;

    public function __construct() {
        $args = func_get_args();

        if (count(array_filter(array_map('is_array', $args))) !== func_num_args()) {
            throw new Exception('Can only pass array arguments');
        }

        $this->_collections = $args;
    }

    protected function _getBatch(array $batch, $index) {
        if ($index < count($this->_collections)) {
            foreach ($this->_collections[$index] as $element) {
                // get combinations of subsequent collections
                $this->_getBatch(array_merge($batch, array($element)), $index + 1);
            }
        } else {
            // got a full combination 
            $this->_combinations[] = $batch;
        }
    }

    public function getCombinations() {
        if (null === $this->_combinations) {
            $this->_getBatch(array(), 0);
        }

        return $this->_combinations;
    }
}

$hats = array('Hat A', 'Hat B', 'Hat C');
$shirts = array('Shirt A', 'Shirt B', 'Shirt C');
$pants = array('Pants A', 'Pants B', 'Pants C');

$combiner = new Combiner($hats, $shirts, $pants);
var_dump($combiner->getCombinations());

它沿着类型列表移动并选择一个类型(比如Hat A),然后递归地构建与该项目相关的其余类型的组合。要添加新类型,只需将另一个参数传递给构造函数即可。

票数 1
EN

Stack Overflow用户

发布于 2012-01-06 21:30:50

首先,将你的数组排列成相似的内容(即所有的帽子在一个数组中,所有的衬衫在另一个数组中,等等),以获得以下结果:

代码语言:javascript
运行
复制
$hats[0] = 'Hat A';
$hats[1] = 'Hat B';
$hats[2] = 'Hat C';

$shirts[0] = 'Shirt A';
$shirts[1] = 'Shirt B';
$shirts[2] = 'Shirt C';

$pants[0] = 'Pants A';
$pants[1] = 'Pants B';
$pants[2] = 'Pants C';

$recipe = array();

然后使用foreach构造遍历每个元素,如下所示:

代码语言:javascript
运行
复制
foreach ($hats as $hat) {
    foreach ($shirts as $shirt) {
        foreach ($pants as $pant) {
            $recipe = $hat." ".$shirt." ".$pant;
        }
    }
}

foreach ($recipe as $r) {
    echo $r."<br>";
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8757320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档