Reputation: 57
I have a function where I can pass a string and a desired length to get all permutations with a fixed length from the characters of the string.
But now I want the permutations of entire words, e.g.
$source = "apple,potatoes,orange";
Sadly this function only gives me the permutations with characters and not entire words and I don't know how to modify the code, so I would get these permutations out of the above example data:
apple,apple
apple,potatoes
apple,orange
potatoes,apple
potatoes,potatoes
potatoes,orange
//...
Code:
<?php
$source = 'AaBbCcDdEe';
foreach(combos_with_repetition($source, 2) as $combo) {
echo "$combo<br>\n";
}
function combos_with_repetition($input, $combo_len = 2)
{
for($i = 0; $i < $combo_len; ++$i)
{
@$part1 .= 'for($k'.$i.' = 0, $len = strlen($input); $k'.$i.' < $len; ++$k'.$i.') ';
@$part2 .= ($i?'.':'') . '$input[$k'.$i.']';
}
eval($part1.'$rtn[] = '.$part2.';');
return $rtn;
}
?>
So any help or hints how to modify the code would help.
Upvotes: 1
Views: 1218
Reputation: 59691
This should work for you and even without evil()
.
So what does this code do?
Pretty simple:
nl = amount of permutations
Where n
is the amount of words and l
the desired length of each combination.
So for this specific example there are 3 words (apple
, patatoes
and orange
) and we want each permutation with a length of 3. Means:
33 = 27 permutations
We loop through all our permutations, which we already have(Starting off with one permutation, an "empty permutation" ($permutations = [[]];
)), and for each permutation we go through our data array and combine each permutation with each input data to a new permutation.
Now we do this until we get the desired length for each permutation.
2.1 Example
Input data:
[1, 2] //Input array with the data
length = 2 //Desired length for each permutation
//↓ new permutations for the next iteration
│
iteration 0:
Permutations:
- [] │ -> []
│
iteration 1: ┌─────────────┤
│ │
Permutations: v v
- [] + 1 │ -> [1]
- [] + 2 │ -> [2]
│
iteration 2: ┌─────────────┤
│ │
Permutations: v v
- [] + 1 │ -> [1]
- [] + 2 │ -> [2]
- [1] + 1 │ -> [1,1] //desired length 2
- [1] + 2 │ -> [1,2] //desired length 2
- [2] + 1 │ -> [2,1] //desired length 2
- [2] + 2 │ -> [2,2] //desired length 2
//↑ All permutations here
So as you can see in the above example we now have all permutations with the desired length which we want, here 2.
But to get only the permutations with the desired length we are overwriting the result array each iteration, so that at the end only the permutations with the expected length are in the results array.
<?php
function getPermutations($input = [], $length = 2, $delimiter = ",") {
$permutations = [[]];
$data = is_array($input) ? $input : explode($delimiter, $input);
for ($count = 0; $count < $length; $count++) {
$tmp = [];
foreach ($permutations as $permutation) {
foreach ($data as $inputValue)
$tmp[] = array_merge($permutation, [$inputValue]);
}
$permutations = $tmp;
}
return $permutations;
}
$result = getPermutations("apple,patatoes,orange", 3);
print_r($result);
?>
output:
Array
(
[0] => Array
(
[0] => apple
[1] => apple
[2] => apple
)
//...
[26] => Array
(
[0] => orange
[1] => orange
[2] => orange
)
)
Upvotes: 4
Reputation: 552
this should do the job for you :
function test($source){
$source_array = explode(',',$source);
$result = '';
foreach($source_array as $item)
{
foreach($source_array as $item2){
$result .= $item.','.$item2.'<br>';
}
}
return $result;
}
$source="apple,patatoes,orange";
echo test($source);
Upvotes: 1