PHP

时间:2015-05-04 10:21:43

标签: php arrays

我需要一个函数来组合数组中的单词。然而,我已经尝试过递归,但还没有得到它。这是我的示例数组:

[1]=> array(4) { [0]=> string(2) "ar" [1]=> string(2) "to" [2]=> string(4) "tron" [3]=> string(3) "var" } 
[2]=> array(1) { [0]=> string(2) "to" } 
[3]=> array(1) { [0]=> string(4) "tron" } 
[4]=> array(4) { [0]=> string(2) "ar" [1]=> string(2) "to" [2]=> string(4) "tron" [3]=> string(3) "var" }

这意味着,在第1位,其中一个字符串" ar","到"," tron"和" var"可以发生。在第二个位置只有String" to"可以发生。等等。

单词的长度应该是数组的长度(在本例中为4)。所有可能的单词都应该作为数组返回。例如:

["artotronar", "artotronto", "artotrontron", "artotronvar", "tototronar", ...]

我的想法是写一个递归函数,但我没有成功。 : - (

最好的问候

理查德

3 个答案:

答案 0 :(得分:2)

我认为这就是你要找的东西:

<?php

$syllables = array(
    array('ar', 'to', 'tron', 'var'),
    array('to'),
    array('tron'),
    array('ar', 'to', 'tron', 'var'),
);
$words = array();
$k = 0;
$max = 0;
for ($i = 1; $i < count($syllables); $i++) {
    $max = max($max, count($syllables[$i]));
}
foreach ($syllables[0] as $syllable) {
    for ($i = 0; $i < $max; $i++) {
        $words[$k] = $syllable;
        for ($j = 1; $j < count($syllables); $j++) {
            $words[$k] .= $syllables[$j][min($i, count($syllables[$j]) - 1)];
        }
        $k++;
    }
}

var_dump($words);

修改

这是一个适用于所有输入并生成所有可能组合的解决方案。该代码假定$ syllables至少有一个数组。

<?php

$syllables = array(
    array('ar', 'to', 'tron', 'var'),
    array('to'),
    array('tron'),
    array('ar', 'to', 'tron', 'var'),
);

$p = 1;
foreach ($syllables as $syllableSet) {
    $p = $p * count($syllableSet);
}
$words = array();
$n0 = count($syllables[0]);
for ($i = 0; $i < $p; $i++) {
    $words[$i] = $syllables[0][$i % $n0];
}

for ($i = 1; $i < $n0; $i++) {
    $pos = 0;
    $ni = count($syllables[$i]);
    for ($k = 0; $k < $p / $n0; $k++) {
        for ($j = 0; $j < $n0; $j++) {
            $words[$pos] .= $syllables[$i][$k % $ni];
            $pos++;
        }
    }
}
var_dump($words);

答案 1 :(得分:1)

我认为这是解决问题的方法:

$pattern[] = array("ar", "to", "tron", "var");
$pattern[] = array("to");
$pattern[] = array("tron");
$pattern[] = array("ar", "to", "tron", "var");

$words = array();

foreach($pattern[0] as $p0) {
    foreach($pattern[1] as $p1) {
        foreach($pattern[2] as $p2) {
            foreach($pattern[3] as $p3) {
                $words[] = $p0.$p1.$p2.$p3;
            }
        }
    }
}

echo "<pre>";
print_r($words);
echo "</pre>";

这将输出artotronvar,artotronar等所有可能的组合......

但我没有做一个递归函数来调用这些......

以下是输出:

Array
(
    [0] => artotronar
    [1] => artotronto
    [2] => artotrontron
    [3] => artotronvar
    [4] => tototronar
    [5] => tototronto
    [6] => tototrontron
    [7] => tototronvar
    [8] => trontotronar
    [9] => trontotronto
    [10] => trontotrontron
    [11] => trontotronvar
    [12] => vartotronar
    [13] => vartotronto
    [14] => vartotrontron
    [15] => vartotronvar
)

答案 2 :(得分:0)

好的,这是我的代码,我重新编写了顶部的数组,这显然是你不必做的,但我把它留在那里,以便你可以看到它的样子。

&#13;
&#13;
<?php

//The array that we are starting with
$parts = array(
    array(
        'ar',
        'to',
        'tron',
        'var',
    ),
    array(
        'to',
    ),
    array(
        'tron',
    ),
    array(
        'ar',
        'to',
        'tron',
        'var',
    ),
);

//Function to do our work
function makewords($parts){
    //Set up an empty array to use as well as a word string for us to manipulate
    $words = array();
    $word = null;
    
    //For each entry in this array
    foreach($parts as $x){
        
        //If this entry is also an array (it should be but this is just to make sure)
        if(is_array($x)){
            
            //Then for each of these entries...
            foreach($x as $y){
                //Create the word
                $word .= $y;
            }
            
            //At this point our word is finished, so add it to the words array
            $words[] = $word;
        }
        
        //Clear $word for the next one
        $word = null;
    }
    
    //Now our array is done, so let's return it
    return $words;
}

//Print it to check that it works
print_r(makewords($parts));
&#13;
&#13;
&#13;

我希望这适合你。

要将其添加到数据库,只需添加数据库插入代码,其中&#34;返回$ words&#34;但是,我是这样做的,然后将其输入到函数的数据库OUTSIDE中,否则该函数将在每次使用时插入;通过这种方式,您可以使用该功能创建阵列,然后随意使用它。