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
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);
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
)
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.
Related
Given a PHP array of strings, e.g.:
['peter', 'paul', 'mary']
How to generate all possible permutations of elements of this array? i.e.:
peter-paul-mary
peter-mary-paul
paul-peter-mary
paul-mary-peter
mary-peter-paul
mary-paul-peter
function pc_permute($items, $perms = array()) {
if (empty($items)) {
echo join(' ', $perms) . "<br />";
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
pc_permute($newitems, $newperms);
}
}
}
$arr = array('peter', 'paul', 'mary');
pc_permute($arr);
or
function pc_next_permutation($p, $size) {
// slide down the array looking for where we're smaller than the next guy
for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { }
// if this doesn't occur, we've finished our permutations
// the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
if ($i == -1) { return false; }
// slide down the array looking for a bigger number than what we found before
for ($j = $size; $p[$j] <= $p[$i]; --$j) { }
// swap them
$tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
// now reverse the elements in between by swapping the ends
for (++$i, $j = $size; $i < $j; ++$i, --$j) {
$tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
}
return $p;
}
$set = split(' ', 'she sells seashells'); // like array('she', 'sells', 'seashells')
$size = count($set) - 1;
$perm = range(0, $size);
$j = 0;
do {
foreach ($perm as $i) { $perms[$j][] = $set[$i]; }
} while ($perm = pc_next_permutation($perm, $size) and ++$j);
foreach ($perms as $p) {
print join(' ', $p) . "\n";
}
http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm
This does what you need, in place, i.e. without allocating any additional memory. It stores the resulting permutations the $results array. I am pretty confident that this is the fasted way to solve the task.
<?php
function computePermutations($array) {
$result = [];
$recurse = function($array, $start_i = 0) use (&$result, &$recurse) {
if ($start_i === count($array)-1) {
array_push($result, $array);
}
for ($i = $start_i; $i < count($array); $i++) {
//Swap array value at $i and $start_i
$t = $array[$i]; $array[$i] = $array[$start_i]; $array[$start_i] = $t;
//Recurse
$recurse($array, $start_i + 1);
//Restore old order
$t = $array[$i]; $array[$i] = $array[$start_i]; $array[$start_i] = $t;
}
};
$recurse($array);
return $result;
}
$results = computePermutations(array('foo', 'bar', 'baz'));
print_r($results);
This works in PHP>5.4. I used a anonymous function for recursion to keep the main function's interface clean.
I needed something similar and found this post while looking. Landed up writing the following which does the job.
With 8 items it works fairly quickly (a bit quicker than the examples I found online), but go beyond that and the run time ramps up rapidly. If you only need to output the results it could be made quicker and the memory use reduced massively.
print_r(AllPermutations(array('peter', 'paul', 'mary')));
function AllPermutations($InArray, $InProcessedArray = array())
{
$ReturnArray = array();
foreach($InArray as $Key=>$value)
{
$CopyArray = $InProcessedArray;
$CopyArray[$Key] = $value;
$TempArray = array_diff_key($InArray, $CopyArray);
if (count($TempArray) == 0)
{
$ReturnArray[] = $CopyArray;
}
else
{
$ReturnArray = array_merge($ReturnArray, AllPermutations($TempArray, $CopyArray));
}
}
return $ReturnArray;
}
Note that the number of permutations is the factorial of the number of items in the array. For 3 items there are 6 permutations, for 4 there are 24, for 5 there are 120, for 6 there are 720, etc.
EDIT
Came back to have a look at this and done some revisions.
Below is an improved version of this function, which uses less storage and is quicker (quicker than other solutions I have seen).
It takes the return array as a parameter, passing it through by reference. This reduces the amount of duplication of data as it runs through.
function AllPermutations($InArray, &$ReturnArray = array(), $InProcessedArray = array())
{
if (count($InArray) == 1)
{
$ReturnArray[] = array_merge($InProcessedArray, $InArray);
}
else
{
foreach($InArray as $Key=>$value)
{
$CopyArray = $InArray;
unset($CopyArray[$Key]);
AllPermutations2($CopyArray, $ReturnArray, array_merge($InProcessedArray, array($Key=>$value)));
}
}
}
I expanded a bit on the answer of Jack
function pc_permute($items, $perms = [],&$ret = []) {
if (empty($items)) {
$ret[] = $perms;
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
$this->pc_permute($newitems, $newperms,$ret);
}
}
return $ret;
}
This will actually return an array with all possible permutations.
$options = ['startx','starty','startz','endx','endy','endz'];
$x = $this->pc_permute($options);
var_dump($x);
[0]=>
array(6) {
[0]=>
string(6) "startx"
[1]=>
string(6) "starty"
[2]=>
string(6) "startz"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[1]=>
array(6) {
[0]=>
string(6) "starty"
[1]=>
string(6) "startx"
[2]=>
string(6) "startz"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[2]=>
array(6) {
[0]=>
string(6) "startx"
[1]=>
string(6) "startz"
[2]=>
string(6) "starty"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[3]=>
array(6) {
[0]=>
string(6) "startz"
[1]=>
string(6) "startx"
[2]=>
string(6) "starty"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[4]=>
array(6) {
[0]=>
string(6) "starty"
[1]=>
string(6) "startz"
[2]=>
string(6) "startx"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[5]=>
array(6) {
[0]=>
string(6) "startz"
[1]=>
string(6) "starty"
[2]=>
string(6) "startx"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[6]=> ................ a lot more
I found it a bit more usefull to get an array back instead of a string. Then it's up to the using application how to handle the resutls(to join them, or something else)
Simple version with recursion and no artificial extra arguments:
function permuteArray(array $input) {
$input = array_values($input);
// permutation of 1 value is the same value
if (count($input) === 1) {
return array($input);
}
// to permute multiple values, pick a value to put in the front and
// permute the rest; repeat this with all values of the original array
$result = [];
for ($i = 0; $i < count($input); $i++) {
$copy = $input;
$value = array_splice($copy, $i, 1);
foreach (permuteArray($copy) as $permutation) {
array_unshift($permutation, $value[0]);
$result[] = $permutation;
}
}
return $result;
}
This algorithm is nice and instructive how you would do it on paper, but otherwise very inefficient as it calculates same permutations multiple times. Not to say that it is very impractical for calculating permutations of larger arrays as the space and number of calculations grow exponentially.
Recursive function to get all permutations of an array.
Call getPermutations($arr) to get an array of arrays with all the permutations.
function getPermutations ($arr)
{
assert (!empty($arr));
if (count($arr)==1)
{
return [$arr];
}
$first=array_shift($arr);
$permutations=getPermutations($arr);
$result=[];
foreach ($permutations as $permutation)
{
$result=array_merge($result, addElementInAllPositions($permutation, $first));
}
return $result;
}
function addElementInAllPositions ($arr, $element)
{
$i=0;
$result=[];
while ($i<=count($arr))
{
$result[]=array_merge(array_slice($arr,0,$i), [$element], array_slice($arr, $i));
$i++;
}
return $result;
}
Here is another variant based on this article: https://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm
public static function get_array_orders( $arr ) {
$arr = array_values( $arr ); // Make sure array begins from 0.
$size = count( $arr ) - 1;
$order = range( 0, $size );
$i = 0;
$orders = [];
do {
foreach ( $order as $key ) {
$orders[ $i ][] = $arr[ $key ];
}
$i ++;
} while ( $order = self::get_next_array_order( $order, $size ) );
return $orders;
}
protected static function get_next_array_order( $order, $size ) {
// slide down the array looking for where we're smaller than the next guy
$i = $size - 1;
while ( isset( $order[ $i ] ) && $order[ $i ] >= $order[ $i + 1 ] ) {
$i --;
}
// if this doesn't occur, we've finished our permutations, the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
if ( $i == - 1 ) {
return false;
}
// slide down the array looking for a bigger number than what we found before
$j = $size;
while( $order[ $j ] <= $order[ $i ] ){
$j--;
}
// swap them
$tmp = $order[ $i ];
$order[ $i ] = $order[ $j ];
$order[ $j ] = $tmp;
// now reverse the elements in between by swapping the ends
for ( ++ $i, $j = $size; $i < $j; ++ $i, -- $j ) {
$tmp = $order[ $i ];
$order[ $i ] = $order[ $j ];
$order[ $j ] = $tmp;
}
return $order;
}
Example:
$langs = ['en', 'fr', 'ru'];
$orders = self::get_array_orders( $langs );
print_r($orders);
Outputs:
Array (
[0] => Array
(
[0] => en
[1] => fr
[2] => ru
)
[1] => Array
(
[0] => en
[1] => ru
[2] => fr
)
[2] => Array
(
[0] => fr
[1] => en
[2] => ru
)
[3] => Array
(
[0] => fr
[1] => ru
[2] => en
)
[4] => Array
(
[0] => ru
[1] => en
[2] => fr
)
[5] => Array
(
[0] => ru
[1] => fr
[2] => en
)
)
Given a PHP array of strings, e.g.:
['peter', 'paul', 'mary']
How to generate all possible permutations of elements of this array? i.e.:
peter-paul-mary
peter-mary-paul
paul-peter-mary
paul-mary-peter
mary-peter-paul
mary-paul-peter
function pc_permute($items, $perms = array()) {
if (empty($items)) {
echo join(' ', $perms) . "<br />";
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
pc_permute($newitems, $newperms);
}
}
}
$arr = array('peter', 'paul', 'mary');
pc_permute($arr);
or
function pc_next_permutation($p, $size) {
// slide down the array looking for where we're smaller than the next guy
for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { }
// if this doesn't occur, we've finished our permutations
// the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
if ($i == -1) { return false; }
// slide down the array looking for a bigger number than what we found before
for ($j = $size; $p[$j] <= $p[$i]; --$j) { }
// swap them
$tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
// now reverse the elements in between by swapping the ends
for (++$i, $j = $size; $i < $j; ++$i, --$j) {
$tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
}
return $p;
}
$set = split(' ', 'she sells seashells'); // like array('she', 'sells', 'seashells')
$size = count($set) - 1;
$perm = range(0, $size);
$j = 0;
do {
foreach ($perm as $i) { $perms[$j][] = $set[$i]; }
} while ($perm = pc_next_permutation($perm, $size) and ++$j);
foreach ($perms as $p) {
print join(' ', $p) . "\n";
}
http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm
This does what you need, in place, i.e. without allocating any additional memory. It stores the resulting permutations the $results array. I am pretty confident that this is the fasted way to solve the task.
<?php
function computePermutations($array) {
$result = [];
$recurse = function($array, $start_i = 0) use (&$result, &$recurse) {
if ($start_i === count($array)-1) {
array_push($result, $array);
}
for ($i = $start_i; $i < count($array); $i++) {
//Swap array value at $i and $start_i
$t = $array[$i]; $array[$i] = $array[$start_i]; $array[$start_i] = $t;
//Recurse
$recurse($array, $start_i + 1);
//Restore old order
$t = $array[$i]; $array[$i] = $array[$start_i]; $array[$start_i] = $t;
}
};
$recurse($array);
return $result;
}
$results = computePermutations(array('foo', 'bar', 'baz'));
print_r($results);
This works in PHP>5.4. I used a anonymous function for recursion to keep the main function's interface clean.
I needed something similar and found this post while looking. Landed up writing the following which does the job.
With 8 items it works fairly quickly (a bit quicker than the examples I found online), but go beyond that and the run time ramps up rapidly. If you only need to output the results it could be made quicker and the memory use reduced massively.
print_r(AllPermutations(array('peter', 'paul', 'mary')));
function AllPermutations($InArray, $InProcessedArray = array())
{
$ReturnArray = array();
foreach($InArray as $Key=>$value)
{
$CopyArray = $InProcessedArray;
$CopyArray[$Key] = $value;
$TempArray = array_diff_key($InArray, $CopyArray);
if (count($TempArray) == 0)
{
$ReturnArray[] = $CopyArray;
}
else
{
$ReturnArray = array_merge($ReturnArray, AllPermutations($TempArray, $CopyArray));
}
}
return $ReturnArray;
}
Note that the number of permutations is the factorial of the number of items in the array. For 3 items there are 6 permutations, for 4 there are 24, for 5 there are 120, for 6 there are 720, etc.
EDIT
Came back to have a look at this and done some revisions.
Below is an improved version of this function, which uses less storage and is quicker (quicker than other solutions I have seen).
It takes the return array as a parameter, passing it through by reference. This reduces the amount of duplication of data as it runs through.
function AllPermutations($InArray, &$ReturnArray = array(), $InProcessedArray = array())
{
if (count($InArray) == 1)
{
$ReturnArray[] = array_merge($InProcessedArray, $InArray);
}
else
{
foreach($InArray as $Key=>$value)
{
$CopyArray = $InArray;
unset($CopyArray[$Key]);
AllPermutations2($CopyArray, $ReturnArray, array_merge($InProcessedArray, array($Key=>$value)));
}
}
}
I expanded a bit on the answer of Jack
function pc_permute($items, $perms = [],&$ret = []) {
if (empty($items)) {
$ret[] = $perms;
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
$this->pc_permute($newitems, $newperms,$ret);
}
}
return $ret;
}
This will actually return an array with all possible permutations.
$options = ['startx','starty','startz','endx','endy','endz'];
$x = $this->pc_permute($options);
var_dump($x);
[0]=>
array(6) {
[0]=>
string(6) "startx"
[1]=>
string(6) "starty"
[2]=>
string(6) "startz"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[1]=>
array(6) {
[0]=>
string(6) "starty"
[1]=>
string(6) "startx"
[2]=>
string(6) "startz"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[2]=>
array(6) {
[0]=>
string(6) "startx"
[1]=>
string(6) "startz"
[2]=>
string(6) "starty"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[3]=>
array(6) {
[0]=>
string(6) "startz"
[1]=>
string(6) "startx"
[2]=>
string(6) "starty"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[4]=>
array(6) {
[0]=>
string(6) "starty"
[1]=>
string(6) "startz"
[2]=>
string(6) "startx"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[5]=>
array(6) {
[0]=>
string(6) "startz"
[1]=>
string(6) "starty"
[2]=>
string(6) "startx"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[6]=> ................ a lot more
I found it a bit more usefull to get an array back instead of a string. Then it's up to the using application how to handle the resutls(to join them, or something else)
Simple version with recursion and no artificial extra arguments:
function permuteArray(array $input) {
$input = array_values($input);
// permutation of 1 value is the same value
if (count($input) === 1) {
return array($input);
}
// to permute multiple values, pick a value to put in the front and
// permute the rest; repeat this with all values of the original array
$result = [];
for ($i = 0; $i < count($input); $i++) {
$copy = $input;
$value = array_splice($copy, $i, 1);
foreach (permuteArray($copy) as $permutation) {
array_unshift($permutation, $value[0]);
$result[] = $permutation;
}
}
return $result;
}
This algorithm is nice and instructive how you would do it on paper, but otherwise very inefficient as it calculates same permutations multiple times. Not to say that it is very impractical for calculating permutations of larger arrays as the space and number of calculations grow exponentially.
Recursive function to get all permutations of an array.
Call getPermutations($arr) to get an array of arrays with all the permutations.
function getPermutations ($arr)
{
assert (!empty($arr));
if (count($arr)==1)
{
return [$arr];
}
$first=array_shift($arr);
$permutations=getPermutations($arr);
$result=[];
foreach ($permutations as $permutation)
{
$result=array_merge($result, addElementInAllPositions($permutation, $first));
}
return $result;
}
function addElementInAllPositions ($arr, $element)
{
$i=0;
$result=[];
while ($i<=count($arr))
{
$result[]=array_merge(array_slice($arr,0,$i), [$element], array_slice($arr, $i));
$i++;
}
return $result;
}
Here is another variant based on this article: https://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm
public static function get_array_orders( $arr ) {
$arr = array_values( $arr ); // Make sure array begins from 0.
$size = count( $arr ) - 1;
$order = range( 0, $size );
$i = 0;
$orders = [];
do {
foreach ( $order as $key ) {
$orders[ $i ][] = $arr[ $key ];
}
$i ++;
} while ( $order = self::get_next_array_order( $order, $size ) );
return $orders;
}
protected static function get_next_array_order( $order, $size ) {
// slide down the array looking for where we're smaller than the next guy
$i = $size - 1;
while ( isset( $order[ $i ] ) && $order[ $i ] >= $order[ $i + 1 ] ) {
$i --;
}
// if this doesn't occur, we've finished our permutations, the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
if ( $i == - 1 ) {
return false;
}
// slide down the array looking for a bigger number than what we found before
$j = $size;
while( $order[ $j ] <= $order[ $i ] ){
$j--;
}
// swap them
$tmp = $order[ $i ];
$order[ $i ] = $order[ $j ];
$order[ $j ] = $tmp;
// now reverse the elements in between by swapping the ends
for ( ++ $i, $j = $size; $i < $j; ++ $i, -- $j ) {
$tmp = $order[ $i ];
$order[ $i ] = $order[ $j ];
$order[ $j ] = $tmp;
}
return $order;
}
Example:
$langs = ['en', 'fr', 'ru'];
$orders = self::get_array_orders( $langs );
print_r($orders);
Outputs:
Array (
[0] => Array
(
[0] => en
[1] => fr
[2] => ru
)
[1] => Array
(
[0] => en
[1] => ru
[2] => fr
)
[2] => Array
(
[0] => fr
[1] => en
[2] => ru
)
[3] => Array
(
[0] => fr
[1] => ru
[2] => en
)
[4] => Array
(
[0] => ru
[1] => en
[2] => fr
)
[5] => Array
(
[0] => ru
[1] => fr
[2] => en
)
)
Given a PHP array of strings, e.g.:
['peter', 'paul', 'mary']
How to generate all possible permutations of elements of this array? i.e.:
peter-paul-mary
peter-mary-paul
paul-peter-mary
paul-mary-peter
mary-peter-paul
mary-paul-peter
function pc_permute($items, $perms = array()) {
if (empty($items)) {
echo join(' ', $perms) . "<br />";
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
pc_permute($newitems, $newperms);
}
}
}
$arr = array('peter', 'paul', 'mary');
pc_permute($arr);
or
function pc_next_permutation($p, $size) {
// slide down the array looking for where we're smaller than the next guy
for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { }
// if this doesn't occur, we've finished our permutations
// the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
if ($i == -1) { return false; }
// slide down the array looking for a bigger number than what we found before
for ($j = $size; $p[$j] <= $p[$i]; --$j) { }
// swap them
$tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
// now reverse the elements in between by swapping the ends
for (++$i, $j = $size; $i < $j; ++$i, --$j) {
$tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
}
return $p;
}
$set = split(' ', 'she sells seashells'); // like array('she', 'sells', 'seashells')
$size = count($set) - 1;
$perm = range(0, $size);
$j = 0;
do {
foreach ($perm as $i) { $perms[$j][] = $set[$i]; }
} while ($perm = pc_next_permutation($perm, $size) and ++$j);
foreach ($perms as $p) {
print join(' ', $p) . "\n";
}
http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm
This does what you need, in place, i.e. without allocating any additional memory. It stores the resulting permutations the $results array. I am pretty confident that this is the fasted way to solve the task.
<?php
function computePermutations($array) {
$result = [];
$recurse = function($array, $start_i = 0) use (&$result, &$recurse) {
if ($start_i === count($array)-1) {
array_push($result, $array);
}
for ($i = $start_i; $i < count($array); $i++) {
//Swap array value at $i and $start_i
$t = $array[$i]; $array[$i] = $array[$start_i]; $array[$start_i] = $t;
//Recurse
$recurse($array, $start_i + 1);
//Restore old order
$t = $array[$i]; $array[$i] = $array[$start_i]; $array[$start_i] = $t;
}
};
$recurse($array);
return $result;
}
$results = computePermutations(array('foo', 'bar', 'baz'));
print_r($results);
This works in PHP>5.4. I used a anonymous function for recursion to keep the main function's interface clean.
I needed something similar and found this post while looking. Landed up writing the following which does the job.
With 8 items it works fairly quickly (a bit quicker than the examples I found online), but go beyond that and the run time ramps up rapidly. If you only need to output the results it could be made quicker and the memory use reduced massively.
print_r(AllPermutations(array('peter', 'paul', 'mary')));
function AllPermutations($InArray, $InProcessedArray = array())
{
$ReturnArray = array();
foreach($InArray as $Key=>$value)
{
$CopyArray = $InProcessedArray;
$CopyArray[$Key] = $value;
$TempArray = array_diff_key($InArray, $CopyArray);
if (count($TempArray) == 0)
{
$ReturnArray[] = $CopyArray;
}
else
{
$ReturnArray = array_merge($ReturnArray, AllPermutations($TempArray, $CopyArray));
}
}
return $ReturnArray;
}
Note that the number of permutations is the factorial of the number of items in the array. For 3 items there are 6 permutations, for 4 there are 24, for 5 there are 120, for 6 there are 720, etc.
EDIT
Came back to have a look at this and done some revisions.
Below is an improved version of this function, which uses less storage and is quicker (quicker than other solutions I have seen).
It takes the return array as a parameter, passing it through by reference. This reduces the amount of duplication of data as it runs through.
function AllPermutations($InArray, &$ReturnArray = array(), $InProcessedArray = array())
{
if (count($InArray) == 1)
{
$ReturnArray[] = array_merge($InProcessedArray, $InArray);
}
else
{
foreach($InArray as $Key=>$value)
{
$CopyArray = $InArray;
unset($CopyArray[$Key]);
AllPermutations2($CopyArray, $ReturnArray, array_merge($InProcessedArray, array($Key=>$value)));
}
}
}
I expanded a bit on the answer of Jack
function pc_permute($items, $perms = [],&$ret = []) {
if (empty($items)) {
$ret[] = $perms;
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
$this->pc_permute($newitems, $newperms,$ret);
}
}
return $ret;
}
This will actually return an array with all possible permutations.
$options = ['startx','starty','startz','endx','endy','endz'];
$x = $this->pc_permute($options);
var_dump($x);
[0]=>
array(6) {
[0]=>
string(6) "startx"
[1]=>
string(6) "starty"
[2]=>
string(6) "startz"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[1]=>
array(6) {
[0]=>
string(6) "starty"
[1]=>
string(6) "startx"
[2]=>
string(6) "startz"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[2]=>
array(6) {
[0]=>
string(6) "startx"
[1]=>
string(6) "startz"
[2]=>
string(6) "starty"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[3]=>
array(6) {
[0]=>
string(6) "startz"
[1]=>
string(6) "startx"
[2]=>
string(6) "starty"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[4]=>
array(6) {
[0]=>
string(6) "starty"
[1]=>
string(6) "startz"
[2]=>
string(6) "startx"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[5]=>
array(6) {
[0]=>
string(6) "startz"
[1]=>
string(6) "starty"
[2]=>
string(6) "startx"
[3]=>
string(4) "endx"
[4]=>
string(4) "endy"
[5]=>
string(4) "endz"
}
[6]=> ................ a lot more
I found it a bit more usefull to get an array back instead of a string. Then it's up to the using application how to handle the resutls(to join them, or something else)
Simple version with recursion and no artificial extra arguments:
function permuteArray(array $input) {
$input = array_values($input);
// permutation of 1 value is the same value
if (count($input) === 1) {
return array($input);
}
// to permute multiple values, pick a value to put in the front and
// permute the rest; repeat this with all values of the original array
$result = [];
for ($i = 0; $i < count($input); $i++) {
$copy = $input;
$value = array_splice($copy, $i, 1);
foreach (permuteArray($copy) as $permutation) {
array_unshift($permutation, $value[0]);
$result[] = $permutation;
}
}
return $result;
}
This algorithm is nice and instructive how you would do it on paper, but otherwise very inefficient as it calculates same permutations multiple times. Not to say that it is very impractical for calculating permutations of larger arrays as the space and number of calculations grow exponentially.
Recursive function to get all permutations of an array.
Call getPermutations($arr) to get an array of arrays with all the permutations.
function getPermutations ($arr)
{
assert (!empty($arr));
if (count($arr)==1)
{
return [$arr];
}
$first=array_shift($arr);
$permutations=getPermutations($arr);
$result=[];
foreach ($permutations as $permutation)
{
$result=array_merge($result, addElementInAllPositions($permutation, $first));
}
return $result;
}
function addElementInAllPositions ($arr, $element)
{
$i=0;
$result=[];
while ($i<=count($arr))
{
$result[]=array_merge(array_slice($arr,0,$i), [$element], array_slice($arr, $i));
$i++;
}
return $result;
}
Here is another variant based on this article: https://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm
public static function get_array_orders( $arr ) {
$arr = array_values( $arr ); // Make sure array begins from 0.
$size = count( $arr ) - 1;
$order = range( 0, $size );
$i = 0;
$orders = [];
do {
foreach ( $order as $key ) {
$orders[ $i ][] = $arr[ $key ];
}
$i ++;
} while ( $order = self::get_next_array_order( $order, $size ) );
return $orders;
}
protected static function get_next_array_order( $order, $size ) {
// slide down the array looking for where we're smaller than the next guy
$i = $size - 1;
while ( isset( $order[ $i ] ) && $order[ $i ] >= $order[ $i + 1 ] ) {
$i --;
}
// if this doesn't occur, we've finished our permutations, the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
if ( $i == - 1 ) {
return false;
}
// slide down the array looking for a bigger number than what we found before
$j = $size;
while( $order[ $j ] <= $order[ $i ] ){
$j--;
}
// swap them
$tmp = $order[ $i ];
$order[ $i ] = $order[ $j ];
$order[ $j ] = $tmp;
// now reverse the elements in between by swapping the ends
for ( ++ $i, $j = $size; $i < $j; ++ $i, -- $j ) {
$tmp = $order[ $i ];
$order[ $i ] = $order[ $j ];
$order[ $j ] = $tmp;
}
return $order;
}
Example:
$langs = ['en', 'fr', 'ru'];
$orders = self::get_array_orders( $langs );
print_r($orders);
Outputs:
Array (
[0] => Array
(
[0] => en
[1] => fr
[2] => ru
)
[1] => Array
(
[0] => en
[1] => ru
[2] => fr
)
[2] => Array
(
[0] => fr
[1] => en
[2] => ru
)
[3] => Array
(
[0] => fr
[1] => ru
[2] => en
)
[4] => Array
(
[0] => ru
[1] => en
[2] => fr
)
[5] => Array
(
[0] => ru
[1] => fr
[2] => en
)
)
I have an array :
Array
(
[0] => Array
(
[batch_id] => 1
[seq_id] => 1
[q_id] => 2046
[a1] => 0
[a2] => 1
[a3] => 2
[a4] => 3
[a5] => 4
)
)
I need to minus the value of a1-a5 by 1
desire result(e.g. a1):
array(4) {
["w_id"]=>
string(5) "99911"
["q_id"]=>
string(4) "2046"
["c_id"]=>
string(6) "a1"
["rank"]=>
int(1) "-1"
}
My code is as follow:
$result = mysql_query("Select * from table_1");
while($cr = mysql_fetch_array($result)){
$rr_id = $cr['batch_id'].$cr['seq_id'];
$rid = '999'.$rr_id;
$q_id = $cr['q_id'];
foreach ($cr as $k => $v){
if(preg_match('{^a\d+$}',$k)){
$new_insert[] = array(
'w_id'=>$rid,
'q_id' =>$q_id,
'c_id' =>$k,
'rank'=>$v-1
);
}
}
However, the result of rank becomes
array(4) {
["w_id"]=>
string(5) "99911"
["q_id"]=>
string(4) "2046"
["c_id"]=>
string(6) "a1"
["rank"]=>
int(0)
}
Cannot show the value of rank
Any problem with my code??Can someone answer my question thank you very much
You can use :
$data = array_map(function ($v) {
foreach($v as $k => &$x) {
if (preg_match('{^a\d+$}', $k)) {
$x = $x - 1;
}
}
return $v;
}, $data);
print_r($data);
Live Demo
preg_match('{^Item \d+$}',$k)
You've got wrong pattern inside preg_match (keys you are looking for are 'a1','a2'... not 'item 1', 'item 2'...), just use this one:
preg_match('{^a\d+$}',$k)
Just use loop and pre decrementation
foreach($cr as $k=>&$v)
{
--$v['a1'];--$v['a2'];--$v['a3'];--$v['a4'];--$v['a5'];
}
print_r($cr);
I use $cr as your array.
$cr[] = array("batch_id"=>1, "seq_id"=>1, "q_id"=>'2046', "a1"=>1,"a2"=>1,"a3"=>1,"a4"=>1,"a5"=>1);
foreach($cr as $k=>&$v){
$v['a1'] = $v['a1'] - 1;
$v['a2'] = $v['a2'] - 1;
$v['a3'] = $v['a3'] - 1;
$v['a4'] = $v['a4'] - 1;
$v['a5'] = $v['a5'] - 1;
}
print_r($cr);
I need to reconstruct an array. Here is the original array:
array(8) {
[0] => array(1)
{
["L_TRANSACTIONID0"] => string(17) "62M97388AY676841D"
}
[1] => array(1)
{
["L_TRANSACTIONID1"] => string(17) "9FF44950UY3240528"
}
[2] => array(1)
{
["L_STATUS0"] => string(9) "Completed"
}
[3] => array(1)
{
["L_STATUS1"] => string(9) "Completed"
}
}
I would like to reconstruct it to be as such:
array(2) {
[0] => array(2)
{
["L_TRANSACTIONID0"] => string(17) "62M97388AY676841D"
["L_STATUS0"] => string(9) "Completed"
}
[1] => array(1)
{
["L_TRANSACTIONID1"] => string(17) "9FF44950UY3240528"
["L_STATUS1"] => string(9) "Completed"
}
}
Notice that the KEYS both match with the numeric representation... Is this at all possible?
edit:
here is my code I am using:
foreach($comparison as $key => $val) {
$findme1 = 'L_TRANSACTID'.$i++;
$findme2 = 'L_STATUS'.$c++;
$arrDisable = array($findme1,$findme2);
if( in_array($key, $arrDisable ) ) {
unset( $comparison[ $key ][$val]);
}
if( in_array($key, $arrDisable) ) {
unset( $comparison[ $key ][$val]);
}
}
Try this
$labels = array('L_TRANSACTIONID', 'L_STATUS');
$res = array();
foreach($arr as $val) {
$key = str_replace($labels, '', key($val));
$res[$key] = isset($res[$key]) ? array_merge($res[$key], $val) : $val;
}
print_r($res);
http://codepad.org/MwqTPqtA
If you are certain the the vector cointains pairs L_TRANSACTIONIDn / L_STATUSn keys,that is to say, for each transactionID, there is a corresponding status, what you can do, is to get the number of id/status records (which should equal the length of the initial array, divided by two), and compose the resultin keys, by increasing the current element count.
Could look something like this:
$numItems = sizeof($myInitialArray) / 2;
$newArray = array();
for($i = 0; $i < $numItems; $i++)
{
$itemID = $i * 2; // since we're getting id/status pairs, we're using a step equal to 2
$newArray[] = array(
("L_TRANSACTIONID" . $i) => $myInitialArray[$itemID], // this is the id value
("L_STATUS" . $i) => $myInitialArray[$itemID + 1] // this is the status for that id
);
}
Hope this helps. Have a great day!