Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组和递归迭代_Php_Arrays_Recursion - Fatal编程技术网

PHP数组和递归迭代

PHP数组和递归迭代,php,arrays,recursion,Php,Arrays,Recursion,我有一个场景,我认为需要某种递归迭代,但我不确定,而且我不是递归编码方面的专家,我画的是空白 以下是场景: 我有这样一句话: [He|She] would [like|love|hate] to [Play Golf|Play Tennis|Play Baseball] Today. 我希望PHP解析该短语(括号[]内的每个部分表示该单词或短语的可能变体,每个变体由|管道分隔),并生成所有变体,例如,上面将返回: He would like to Play Golf Today. He wo

我有一个场景,我认为需要某种递归迭代,但我不确定,而且我不是递归编码方面的专家,我画的是空白

以下是场景:

我有这样一句话:

[He|She] would [like|love|hate] to [Play Golf|Play Tennis|Play Baseball] Today.
我希望PHP解析该短语(括号[]内的每个部分表示该单词或短语的可能变体,每个变体由|管道分隔),并生成所有变体,例如,上面将返回:

He would like to Play Golf Today. 
He would like to Play Tennis Today.
He would like to Play Baseball Today.
He would love to Play Golf Today.
He would love to Play Tennis Today.
He would love to Play Baseball Today.
He would hate to Play Golf Today.
He would hate to Play Tennis Today.
He would hate to Play Baseball Today.
She would like to Play Golf Today.
She would like to Play Tennis Today.
She would like to Play Baseball Today.
She would love to Play Golf Today.
She would love to Play Tennis Today.
She would love to Play Baseball Today.
She would hate to Play Golf Today.
She would hate to Play Tennis Today.
She would hate to Play Baseball Today.

我正试图找出如何编写PHP代码来获取输入的短语,并返回所有可能的句子。

这里是一个递归解决方案:

<?php
function generator($input, &$result)
{
    $matches = array();
    if (preg_match('/\[(.*?)\]/', $input, $matches))
    {
        $words = explode('|', $matches[1]);
        $n = count($words);
        for ($i = 0; $i < $n; ++$i)
        {
            $input1 = str_replace($matches[0], $words[$i], $input);
            generator($input1, $result);
        }
    }
    else
    {
        $result[] = $input;
    }
}

$input = '[He|She] would [like|love|hate]';
$result = array();
generator($input, $result);
var_dump($result);

我想使用yeild,但我的php版本(5.4.7)太旧了。

首先,您必须拆分输入字符串并将其重写为数组。这将有如下输出:(我将使用strpos、regexp和/或explode来拆分所有部分)

之后,您必须循环遍历数组,构建所有组合并将它们存储到字符串中。这个看起来像这样

//this is for tracking the progress
for($x = 0; $x < count($array_parts); $x++)
{
    //starting all at the first option
    $array_tracker[$x] = 0;
}

while(true)
{
    //build selected possibility
    $ouput_string = "";
    for($x = 0; $x < count($array_parts); $x++)
    {
        $ouput_string .= $array_parts[$x][$array_tracker[$x]];
    }
    $output_strings[] = $output_string;

    //navigate to next possibility
    for($x = count($array_parts) - 1; $x >= 0; $x--)
    {
        $array_tracker[$x]++;
        if($array_tracker[$x] == count($array_parts[$x))
        {
            $array_tracker[$x] = 0;
        }
        else
        {
            break;
        }
        if($x == 0)
        {
            //all option are done, than end this 'endless' loop
            break 2;
        }
    }
}
//用于跟踪进度
对于($x=0;$x=0;$x--)
{
$array_tracker[$x]++;
如果($array\u tracker[$x]==计数($array\u parts[$x))
{
$array_tracker[$x]=0;
}
其他的
{
打破
}
如果($x==0)
{
//所有选项都完成了,然后结束这个“无止境”循环
破口2;
}
}
}
更新:动态创建循环


演示:

用于生成组合的递归函数:

array(6) {
  [0]=>
  string(13) "He would like"
  [1]=>
  string(13) "He would love"
  [2]=>
  string(13) "He would hate"
  [3]=>
  string(14) "She would like"
  [4]=>
  string(14) "She would love"
  [5]=>
  string(14) "She would hate"
}
function combix($items, $combos=array()){
    $res = array();
    $next = array_shift($items);
    if (is_array($next)){
        if (empty($combos)){
            return combix($items, $next);
        }
        foreach ($combos as $key => $value){
            foreach ($next as $key2 => $value2){
                if (is_array($value)){
                    $res[] = array_merge($value, array($value2));
                } else {
                    $res[] = array($value, $value2);
                }
            }
        }
        return combix($items, $res);
    } else {
        return $combos;
    }
}
文本处理:

$str = '[He|She] would [like|love|hate] to [Play Golf|Play Tennis|Play Baseball] Today.';
$pattern = '#\[([\w\s|]+)]#';
if (preg_match_all($pattern, $str, $matches)){
    $template = preg_replace($pattern, '%s', $str);
    $mix = array();
    foreach ($matches[1] as $key => $value){
        $mix[] = explode('|', $value);
    }
    $res = combix($mix);
    $out = array();
    foreach ($res as $key => $value){
        array_unshift($value, $template);
        $out[] = call_user_func_array('sprintf', $value);
    }
} else {
    $out = array($str);
}
print_r($out);

有限状态机来解析短语并生成所有句子?出于我的目的,我想我们可以限制每个[]组10个可能的项目,以及句子总共10个“片段”(每个片段可以是单数值或[]组)我想,对每个组任意数量的项都可以这样做。你要做的是找到可能输入的笛卡尔积,然后用一个简单的
foreach
生成你想要的输出。有几种好方法可以做到这一点(包括我自己的答案)此处:。如果您需要有关如何执行此操作的更多指导,我可以展开成一个答案。是否有任何方法(使用您的代码)可以解释字符串中多于或少于3个[]括号内的工作组?谢谢!这正是我想要的(尤其是generator()函数)!@onernerd从睡眠中醒来后,我意识到,如何改进该功能。看一看:我摆脱了is_空检查。
Array
(
    [0] => would
    [1] => to
    [2] => Today.
)
Array
(
    [0] => Array
        (
            [0] => He would
            [1] => She would
        )

    [1] => Array
        (
            [0] => like to
            [1] => love to
            [2] => hate to
        )

    [2] => Array
        (
            [0] => Play Golf Today.
            [1] => Play Tennis Today.
            [2] => Play Baseball Today.
        )

)
Array
(
    [0] => He would like to Play Golf Today.
    [1] => He would like to Play Tennis Today.
    [2] => He would like to Play Baseball Today.
    [3] => He would love to Play Golf Today.
    [4] => He would love to Play Tennis Today.
    [5] => He would love to Play Baseball Today.
    [6] => He would hate to Play Golf Today.
    [7] => He would hate to Play Tennis Today.
    [8] => He would hate to Play Baseball Today.
    [9] => She would like to Play Golf Today.
    [10] => She would like to Play Tennis Today.
    [11] => She would like to Play Baseball Today.
    [12] => She would love to Play Golf Today.
    [13] => She would love to Play Tennis Today.
    [14] => She would love to Play Baseball Today.
    [15] => She would hate to Play Golf Today.
    [16] => She would hate to Play Tennis Today.
    [17] => She would hate to Play Baseball Today.
)
function combix($items, $combos=array()){
    $res = array();
    $next = array_shift($items);
    if (is_array($next)){
        if (empty($combos)){
            return combix($items, $next);
        }
        foreach ($combos as $key => $value){
            foreach ($next as $key2 => $value2){
                if (is_array($value)){
                    $res[] = array_merge($value, array($value2));
                } else {
                    $res[] = array($value, $value2);
                }
            }
        }
        return combix($items, $res);
    } else {
        return $combos;
    }
}
$str = '[He|She] would [like|love|hate] to [Play Golf|Play Tennis|Play Baseball] Today.';
$pattern = '#\[([\w\s|]+)]#';
if (preg_match_all($pattern, $str, $matches)){
    $template = preg_replace($pattern, '%s', $str);
    $mix = array();
    foreach ($matches[1] as $key => $value){
        $mix[] = explode('|', $value);
    }
    $res = combix($mix);
    $out = array();
    foreach ($res as $key => $value){
        array_unshift($value, $template);
        $out[] = call_user_func_array('sprintf', $value);
    }
} else {
    $out = array($str);
}
print_r($out);