I have been trying for about 2 days now to get this to work. I am trying to create a permutation function that will take an array and a size as arguments. It will loop through the array and create all the possible permutations with a size of the passed size argument. An example will be as follows. $array = range('A','E'); //any range would be allowed. If I passed this array with a size of 2 I would get the following result... AB, BA AC, CA AD, DA AE, EA BC, CB BD, DB BE, EB CD, DC CE, EC DE, ED and so on... --- If I passed this array with a size of 3 I would get the following result... ABC, ACB BAC, BCA CAB, CBA and so on... -- If I passed this array with a size of 4 I would get the following result... ABCD, ABDC, ABCE, ABEC, ABDE, ABED, ACBD, ACDB, ACBE, ACEB, ACDE, ACED ADBC, ADCB, ADBE, ADEB, ADCE, ADEC and so on for any size passed, unless it isn't possible meaning if the size is larger than the array count. -- Anyone that can help me with this I wouldn't mind a donation on paypal if it works correctly in all aspects.
this works for me. function pc_permute($items, $perms = array( )) { if (empty($items)) { print 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); } } } $value = array('a','b','c','s'); $a = pc_permute($value); print_r($a); PHP: Now to get to the formatting you want, you may have to replace '<br/>' from the function with your delimiter such as ', '
Thanks, but this doesn't allow you to pass a size. An example would be passing 2 with the letters abcs would give me all permutations of 2 with those letters. Example: ab,ac,as,bc,bs and so on. If it was 3 then it would be abc,abs,acb,asb and so on.
This is exactly what I need written in PHP. http://www.mathsisfun.com/combinatorics/combinations-permutations-calculator.html If you select balls on the right side and then put in 16 for choose from and 4 for number chosen and yes on is order important and no on repetition allows and in the list put in 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 and then click the list button. This does exactly what I am looking for.