Richard
Richard

Reputation: 2815

Complex combination of words in PHP

I need a function to combinate words from an array. Yet, I have tried it with recursion but do not have get it yet. This is the example array I have:

[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" }

This means, that at position 1 one of the Strings "ar", "to", "tron" and "var" can take place. On position two only the String "to" can take place. And so on.

The length of the words should be the length of the array (in this case 4). All possible words should be returned as an array. For example here:

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

My idea was to write a recursive function, but I did not succeed in it. :-(

Best Regards

Richard

Upvotes: 0

Views: 116

Answers (3)

littleibex
littleibex

Reputation: 1712

I think this is what you are looking for:

<?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);

EDIT:

Here's a solution that will work for all inputs and generate every possible combination. The code assumes that $syllables will have at least one array.

<?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);

Upvotes: 2

catzilla
catzilla

Reputation: 1981

I think this is the solution to your problem:

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

this will output all possible combinations of artotronvar, artotronar, etc...

but I didn't make a recursive function to call these...

and here are the output:

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
)

Upvotes: 1

Chris Evans
Chris Evans

Reputation: 1265

Okay here's my code, I re-wrote the array at the top which obviously you won't have to do, but I left it in there so that you can see how it looks.

<?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));

I hope this works for you.

To add it to the database, just add your database insert code where "return $words" is, however I'd do it this way and then enter it in to the database OUTSIDE of the function, otherwise the function will make an insert every time it's used; this way you can just use the function to create your array and then do what you please with it.

Upvotes: 0

Related Questions