I am looking for an efficient combinations function that would take into account the order of the array. For example I want to generate all of the combinations of "hello world" but I don't want any of it reversed "world hello". I currently have a combinations function but it's too much.
Simple example :
$text = "Hello World"
$arr = explode(" ",$text); // string to array
And gives:
$result = combo_function($arr);
var_dump($result);
"Hello World"
"Hello"
"World"
I do NOT want the inverse:
"World Hello"
- the key is that I want it to be faster than combinations if possible.
Currently I am using this:
// careful, this is very labor intensive ( O(n^k) )
public function buildSearchCombinations(&$set, &$results)
{
for ($i = 0; $i < count($set); $i++) {
$results[] = (string) $set[$i];
$tempset = $set;
array_splice($tempset, $i, 1);
$tempresults = array();
$this->buildSearchCombinations($tempset, $tempresults);
foreach ($tempresults as $res) {
$results[] = trim((string) $set[$i]) . " " . (string) trim($res);
}
}
}