Get ALL possible results from the mixing array.

I searched everywhere for this online, but could not completely find it. (my PHP and math skills allow me to go down to this ...) I have an array containing, for example, three lines (could also be more!) (For example: "a", "b", "c"). Now I want to create a function that returns ALL possibilities. I searched everywhere and found some good functions that moved the array in every way possible, but they did not delete the value one by one. Thus, they had:

abc acb bac bca cab cba

This is fine, but I need a function that takes it to the next level:

abc acb bac bca cab cba ac ca ab ab ba bc ba abc

and it doesn’t care how many values ​​(say max 10). I struggled with this all evening, maybe someone led me out of my suffering and allowed me this riddle, please? Or give some advice. Thanks

+6
source share
3 answers

As always, it’s much more interesting to solve the problem in your own way. You can change your code to suit your special needs much easier because you know what you are doing :) See My test script below:

<? $a = array("a", "b", "c"); function getAll($prefix, $remaining) { echo $prefix."\n"; if (count($remaining) == 0) return; if (count($remaining) == 1) { echo $prefix.$remaining[0]."\n"; return; } for ($i = 0; $i < count($remaining); $i++) { $t = $remaining; unset($t[$i]); $t = array_values($t); getAll($prefix.$remaining[$i], $t); } } echo "<pre>\n"; getAll('', $a); echo "</pre>\n"; ?> 

I just chose the expected output, instead you can add an array of results, but this is your part :)

Conclusion:

 [] //empty value is included, but blank lines won't show up here, so I wrote [] a ab abc ac acb b ba bac bc bca c ca cab cb cba 
+1
source

Sounds like you're asking for a "power set". The power set includes an empty set, which I will not include.

 require_once 'Math/Combinatorics.php'; $set = range('a', 'c'); $permutationSizes = range(1, count($set)); $combinatorics = new Math_Combinatorics; $result = array(); foreach ($permutationSizes as $permutationSize) { $result = array_merge($result, $combinatorics->permutations($set, $permutationSize)); } print_r($result); 

http://pear.php.net/package/Math_Combinatorics

I assume that technically this is not a set of power, because I do order when comparing sets. anyway...

+1
source

There is an example function for permutations on the O'Reilly PHP Cookbook, section 4.26 . It only creates permutations of a fixed size, but can be used in a loop to work with any size as you need it.

The code

 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 = explode(' ', 'ab c'); // $all will contain the final output $all = $set; while(count($set) > 1) { $perms = array(); $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) { $all[] = implode(' ', $p); } array_pop($set); } // display results foreach($all as $each) { echo $each . "\n"; } 

Exit

 a b c abc acb bac bca cab cba ab ba 

Real time example

0
source

Source: https://habr.com/ru/post/916215/


All Articles