在PHP中创建所有可能的组合 [英] Create every possible combination in PHP

查看:78
本文介绍了在PHP中创建所有可能的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试获取一组单词的每种可能组合的列表。

So, I am trying to get a list of every possible combination of a set of words.

使用输入文本 1、2,并且值是( 1 => a, b, c)和( 2 => d, e),我会得到类似的东西:

With the input text "1, 2" and the values being ("1" => "a", "b", "c") and ("2" => "d", "e"), I would get something like:

a, d
b, d
c, d
a, e
b, e
c, e

使用我现在拥有的代码,我只会得到:

With the code I have right now, I only get:

a, d
b, d
c, d

如何解决这个问题?

代码:

foreach ($words as $word)
{
    for ($i = 0; $i < count($array); $i++) //For every value
    {
        $key = array_keys($array[$i])[0];
        if ($word === $key)
        {
            $syn = explode("|", $array[$i][$key]); //Get all synonyms
            foreach ($syn as $s) //For each synonym within
            {

                $potential[] = implode(" ", str_replace($key, $s, $words));
            }
        }
    }
}

该过程适用于输入文本中的每个单词,我们要遍历整个值数组。在那里,我们还有其他数组(原始 =>同义词)。从那里开始,我们遍历每个同义词并将其添加到可能的组合列表中。

The procedure is for every word in the input text, we want to go through our entire array of values. In there, we have other arrays ("original" => "synonyms"). From there, we loop through every synonym and add it to a list of potential combinations.

推荐答案

其中涉及一些步骤:


  1. 将同义词列表缩小到仅在字符串中找到的那些

  2. 从以下位置创建模板从而构建最终句子

  3. 获取所有同义词组合并将其应用于模板

  1. narrow down the list of synonyms to only those found in the string
  2. create a template from which to build the final sentences
  3. get all synonym combinations and apply them to the template

以下将做到这一点:

$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";
}

演示

这篇关于在PHP中创建所有可能的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆