Combinations, Dispositions and Permutations in PHP

Combinations, Dispositions and Permutations in PHP

Here is code to get all permutations:

http://php.net/manual/en/function.shuffle.php#90615

With the code to get the power set, permutations are those of maximal length, the power set should be all combinations. I have no idea what dispositions are, so if you can explain them, that would help.

Generate combinations in php

    <?php

$array = array('A', 'B', 'C', 'D');

function depth_picker($arr, $temp_string, &$collect) {
if ($temp_string != "")
$collect []= $temp_string;

for ($i=0; $i<sizeof($arr);$i++) {
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1); // removes and returns the i'th element
if (sizeof($arrcopy) > 0) {
depth_picker($arrcopy, $temp_string ." " . $elem[0], $collect);
} else {
$collect []= $temp_string. " " . $elem[0];
}
}
}

$collect = array();
depth_picker($array, "", $collect);
print_r($collect);

?>

The code is get from stackoverflow:
StackOverflow Link

Get all possible combinations of a PHP Array split in two

Just googled and found these results from stackoverflow.com

Get all combinations of a PHP array

algorithm that will take numbers or words and find all possible combinations

PHP: How to get all possible combinations of 1D array?

PHP Find All (somewhat) Unique Combinations of an Array

If you don't mind using a couple of global variables, you could do this in PHP (translated from a version in JavaScript):

<?PHP
$result = array();
$combination = array();

function combinations(array $myArray, $choose) {
global $result, $combination;

$n = count($myArray);

function inner ($start, $choose_, $arr, $n) {
global $result, $combination;

if ($choose_ == 0) array_push($result,$combination);
else for ($i = $start; $i <= $n - $choose_; ++$i) {
array_push($combination, $arr[$i]);
inner($i + 1, $choose_ - 1, $arr, $n);
array_pop($combination);
}
}
inner(0, $choose, $myArray, $n);
return $result;
}

print_r(combinations(array(20,20,22,24), 3));
?>

OUTPUT:

Array ( [0] => Array ( [0] => 20 
[1] => 20
[2] => 22 )
[1] => Array ( [0] => 20
[1] => 20
[2] => 24 )
[2] => Array ( [0] => 20
[1] => 22
[2] => 24 )
[3] => Array ( [0] => 20
[1] => 22
[2] => 24 ) )

PHP: How to get all possible combinations of 1D array?

I believe your professor will be happier with this solution:

<?php

$array = array('Alpha', 'Beta', 'Gamma', 'Sigma');

function depth_picker($arr, $temp_string, &$collect) {
if ($temp_string != "")
$collect []= $temp_string;

for ($i=0, $iMax = sizeof($arr); $i < $iMax; $i++) {
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1); // removes and returns the i'th element
if (sizeof($arrcopy) > 0) {
depth_picker($arrcopy, $temp_string ." " . $elem[0], $collect);
} else {
$collect []= $temp_string. " " . $elem[0];
}
}
}

$collect = array();
depth_picker($array, "", $collect);
print_r($collect);

?>

This solves it:

Array
(
[0] => Alpha
[1] => Alpha Beta
[2] => Alpha Beta Gamma
[3] => Alpha Beta Gamma Sigma
[4] => Alpha Beta Sigma
[5] => Alpha Beta Sigma Gamma
[6] => Alpha Gamma
[7] => Alpha Gamma Beta
[8] => Alpha Gamma Beta Sigma
[9] => Alpha Gamma Sigma
[10] => Alpha Gamma Sigma Beta
[11] => Alpha Sigma
[12] => Alpha Sigma Beta
[13] => Alpha Sigma Beta Gamma
[14] => Alpha Sigma Gamma
[15] => Alpha Sigma Gamma Beta
[16] => Beta
[17] => Beta Alpha
[18] => Beta Alpha Gamma
[19] => Beta Alpha Gamma Sigma
[20] => Beta Alpha Sigma
[21] => Beta Alpha Sigma Gamma
[22] => Beta Gamma
[23] => Beta Gamma Alpha
[24] => Beta Gamma Alpha Sigma
[25] => Beta Gamma Sigma
[26] => Beta Gamma Sigma Alpha
[27] => Beta Sigma
[28] => Beta Sigma Alpha
[29] => Beta Sigma Alpha Gamma
[30] => Beta Sigma Gamma
[31] => Beta Sigma Gamma Alpha
[32] => Gamma
[33] => Gamma Alpha
[34] => Gamma Alpha Beta
[35] => Gamma Alpha Beta Sigma
[36] => Gamma Alpha Sigma
[37] => Gamma Alpha Sigma Beta
[38] => Gamma Beta
[39] => Gamma Beta Alpha
[40] => Gamma Beta Alpha Sigma
[41] => Gamma Beta Sigma
[42] => Gamma Beta Sigma Alpha
[43] => Gamma Sigma
[44] => Gamma Sigma Alpha
[45] => Gamma Sigma Alpha Beta
[46] => Gamma Sigma Beta
[47] => Gamma Sigma Beta Alpha
[48] => Sigma
[49] => Sigma Alpha
[50] => Sigma Alpha Beta
[51] => Sigma Alpha Beta Gamma
[52] => Sigma Alpha Gamma
[53] => Sigma Alpha Gamma Beta
[54] => Sigma Beta
[55] => Sigma Beta Alpha
[56] => Sigma Beta Alpha Gamma
[57] => Sigma Beta Gamma
[58] => Sigma Beta Gamma Alpha
[59] => Sigma Gamma
[60] => Sigma Gamma Alpha
[61] => Sigma Gamma Alpha Beta
[62] => Sigma Gamma Beta
[63] => Sigma Gamma Beta Alpha
)

how do i create all possible letter combinations with php

Ok, because I believe that you don't have any clue what to do, I will give you a short code example. It is not perfect and its written down in a couple of minutes but it should bring you on the right way:

$values = 'abcd';
run(strlen($values), 0 );

function run($length, $pos, $out = '' ) {
global $values;

for ($i = 0; $i < $length; ++$i) {
if ($pos < $length ) {
run($length, $pos + 1, $out . $values[$i]);
}
}
echo $out.PHP_EOL;
}

Next time try first and post your question with the code you have written even if it is nearly nothing and looks stupid for you. But in the end it will show that you make own work and don't hope to get your work done for free without thinking by yourself.

php string permutation

In brute-force fashion:

$chars = array_merge(range('0', '9'), range('a', 'z'), range('A', 'Z'));
$cnt = count($chars);
$strings = array();
for ($first = 0; $first < $cnt; $first++) {
for ($second = 0; $second < $cnt; $second++) {
for ($third= 0; $third< $cnt; $third++) {
for ($fourth= 0; $fourth < $cnt; $fourth++) {
$strings[] = $chars[$first] . $chars[$second] . $chars[$third] . $chars[$fourth];
}
}
}
}

You'll end up with a honkin' big array, so you might want to sprinkle some database operations into one of the loops, so $strings doesn't get too big and kill your script by hitting a memory limit.

How to get ALL combinations of a list of words using ANY number of words

FINAL ANSWER AT THE BOTTOM


Pseudocode (has not been tested)

$str = "apple pear banana";
$str_splode = explode(' ',$str);

echo showCombo($str_splode[0], $str_splode);

function showCombo($str, $arr){
$ret = '';
foreach($arr as $val){
if($val != $str)
$ret .= $str.showCombo($val, $arr);
}
return $ret;
}

Running code: http://codepad.org/IUPJbhI7

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
print_r(showCombo(array(), $str_splode));

function showCombo($str_arr, $arr){
$ret = array();
foreach($arr as $val){
if(!in_array($val, $str_arr)){
$temp = $str_arr;
$temp[] = $val;
print_r($temp);
$comb = showCombo($temp, $arr);
if(count($comb) > 0)
$ret[] = $comb;
}
}
return $ret;
}
?>

This returns all possible combinations


Or this one looks better: http://codepad.org/KCLeRUYs

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
print_r(showCombo(array(), $str_splode));

function showCombo($str_arr, $arr){
$ret = array();
foreach($arr as $val){
if(!in_array($val, $str_arr)){
$temp = $str_arr;
$temp[] = $val;
$ret[$val] = $temp;
$ret[$val][] = showCombo($temp, $arr);
}
}
return $ret;
}

?>

Or if you want to look at flat keys: http://codepad.org/95aNQzXB



Final Answer:

And this one lists them all: http://codepad.org/vndOI9Yj

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
$combos = showCombo(array(), $str_splode);
foreach($combos as $key=>$array){
echo $key.PHP_EOL;
displayArrayByKey($key, $array);
}

function displayArrayByKey($str, $arr){
foreach($arr as $key=>$array){
$string = $str. " " . $key;
echo $string . PHP_EOL;
if(count($array)> 0){
displayArrayByKey($string, $array);
}
}
}

function showCombo($str_arr, $arr){
$ret = array();
foreach($arr as $val){
if(!in_array($val, $str_arr)){
$temp = $str_arr;
$temp[] = $val;
$ret[$val] = showCombo($temp, $arr);
}
}
return $ret;
}

?>

How write all possible words in php?

Version 1:

for($s = 'a'; $s <= 'zzzzzzzzzz'; print $s++.PHP_EOL);

as noted by Paul in comments below, this will only go to zzzzzzzzyz. A bit slower (if anyone cares) but correct version would be:

//modified to include arnaud576875's method of checking exit condition
for($s = 'a'; !isset($s[10]); print $s++.PHP_EOL);


Related Topics



Leave a reply



Submit