所以,我试图得到一组单词的每个可能组合的列表。
输入文本“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
我怎样才能解决这个问题?
代码:
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));
}
}
}
}
该过程适用于输入文本中的每个单词,我们希望遍历整个值数组。在那里,我们有其他数组(“原始”=&gt;“同义词”)。从那里,我们遍历每个同义词并将其添加到潜在组合列表中。
答案 0 :(得分:2)
假设1和2都是数组,你可以嵌套for循环来获得所有可能的组合。
$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);
这将打印出来
Array ( [0] => ad
[1] => ae
[2] => bd
[3] => be
[4] => cd
[5] => ce )
希望这就是你要找的东西。
答案 1 :(得分:2)
涉及以下几个步骤:
以下将会这样做:
$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";
}