在PHP中创建所有可能的组合

在PHP中创建所有可能的组合,php,arrays,combinations,Php,Arrays,Combinations,所以,我试图得到一个列表,列出一组单词的每一个可能的组合 输入文本1,2,值为1=>a,b,c和2=>d,e,我会得到如下结果: a, d b, d c, d a, e b, e c, e 根据我现在掌握的代码,我只得到: a, d b, d c, d 我怎样才能避开这件事 代码: 这个过程是针对输入文本中的每个单词,我们希望遍历整个值数组。在这里,我们有其他数组original=>同义词。从这里开始,我们循环遍历每个同义词,并将其添加到潜在组合列表中。假设1和2都是数组,您可以对循环进行嵌

所以,我试图得到一个列表,列出一组单词的每一个可能的组合

输入文本1,2,值为1=>a,b,c和2=>d,e,我会得到如下结果:

a, d
b, d
c, d
a, e
b, e
c, e
根据我现在掌握的代码,我只得到:

a, d
b, d
c, d
我怎样才能避开这件事

代码:


这个过程是针对输入文本中的每个单词,我们希望遍历整个值数组。在这里,我们有其他数组original=>同义词。从这里开始,我们循环遍历每个同义词,并将其添加到潜在组合列表中。

假设1和2都是数组,您可以对循环进行嵌套,以获得每个可能的组合

$one = array("a", "b", "c");
$two = array("d", "e");


$final_array = array();

for($a = 0; $a<sizeof($one); $a++) 
{
    for($b = 0; $b<sizeof($two); $b++) 
    {

            $final_array[] = $one["$a"] . $two["$b"];
    }
}

print_r($final_array);

希望这就是您想要的。

其中包括几个步骤:

将同义词列表缩小到字符串中的同义词 创建一个模板来构建最后的句子 获取所有同义词并将其应用于模板 以下步骤可以做到这一点:

$dict = [
    '1' => ['a', 'b', 'c'],
    '2' => ['d', 'e'],
];

$str = '2, 1';

class SentenceTemplate implements IteratorAggregate
{
    private $template;
    private $thesaurus;

    public function __construct($str, $dict)
    {
        $this->thesaurus = [];

        $this->template = preg_replace_callback('/\w+/', function($matches) use ($dict) {
            $word = $matches[0];
            if (isset($dict[$word])) {
                $this->thesaurus[] = $dict[$word];
                return '%s';
            } else {
                return $word;
            }
        }, $str);
    }

    public function getIterator()
    {
        return new ArrayIterator(array_map(function($args) {
            return vsprintf($this->template, $args);
        }, $this->combinations($this->thesaurus)));
    }

    private function combinations($arrays, $i = 0) {
        if (!isset($arrays[$i])) {
            return array();
        }
        if ($i == count($arrays) - 1) {
            return $arrays[$i];
        }

        // get combinations from subsequent arrays
        $tmp = $this->combinations($arrays, $i + 1);

        $result = array();

        // concat each array from tmp with each element from $arrays[$i]
        foreach ($arrays[$i] as $v) {
            foreach ($tmp as $t) {
                $result[] = is_array($t) ? array_merge(array($v), $t) : array($v, $t);
            }
        }

        return $result;
    }
}

$sentences = new SentenceTemplate($str, $dict);
foreach ($sentences as $sentence) {
    echo "$sentence\n";
}

这似乎对我不起作用。数据:dict hello=>arrayhey,怎么了…,name=>arraynomenclature。。。str=hello我的名字是-我得到的唯一结果是0=my数组is@AdamM我将用你的输入再次测试它,看看我遗漏了什么:@AdamM我用我的代码运行了你的输入,它工作正常;我做了一个小的更新,因为我没有在底部使用$str。这对我没有帮助,因为我不知道我将有多少个数组。我可能有一个,我可能有一百个。因此,我需要某种可以处理任意数量的函数:foreach循环可能比重复调用sizeof的循环更好。
Array ( [0] => ad 
        [1] => ae 
        [2] => bd 
        [3] => be 
        [4] => cd 
        [5] => ce )
$dict = [
    '1' => ['a', 'b', 'c'],
    '2' => ['d', 'e'],
];

$str = '2, 1';

class SentenceTemplate implements IteratorAggregate
{
    private $template;
    private $thesaurus;

    public function __construct($str, $dict)
    {
        $this->thesaurus = [];

        $this->template = preg_replace_callback('/\w+/', function($matches) use ($dict) {
            $word = $matches[0];
            if (isset($dict[$word])) {
                $this->thesaurus[] = $dict[$word];
                return '%s';
            } else {
                return $word;
            }
        }, $str);
    }

    public function getIterator()
    {
        return new ArrayIterator(array_map(function($args) {
            return vsprintf($this->template, $args);
        }, $this->combinations($this->thesaurus)));
    }

    private function combinations($arrays, $i = 0) {
        if (!isset($arrays[$i])) {
            return array();
        }
        if ($i == count($arrays) - 1) {
            return $arrays[$i];
        }

        // get combinations from subsequent arrays
        $tmp = $this->combinations($arrays, $i + 1);

        $result = array();

        // concat each array from tmp with each element from $arrays[$i]
        foreach ($arrays[$i] as $v) {
            foreach ($tmp as $t) {
                $result[] = is_array($t) ? array_merge(array($v), $t) : array($v, $t);
            }
        }

        return $result;
    }
}

$sentences = new SentenceTemplate($str, $dict);
foreach ($sentences as $sentence) {
    echo "$sentence\n";
}