eviltwinkie Posted April 11, 2007 Share Posted April 11, 2007 Part Deux... I originally wanted to figure a method to better implement a nested foreach to calculate all the permutations held within an array. After posting the topic and getting great responses an elegant solution was found. I then increased the difficulty by requesting that we also include a method to "filter" or "skip" certain values during permutation calculation. The original solution finder quickly came back with yet another code snippet listed below which acomplished the request...it was mere childs play it seems for them (sasa). I marked the topic solved as the original question had been solved. I am creating this new thread as I am once again adding an additional layer of difficulty. The final previous solution is listed first as a reference, followed by the new and more complex problem underneath that one. My attempts to shunt in the functionality have all failed I think due to the recursive nature...any takers? <?php $arrayin=Array ( 'P1' => Array('A', 'B', 'C'), 'P2' => Array('D', 'E', 'F'), 'P3' => Array('G'), 'P4' => Array('H', 'I') ); $skip = array('P1' => array('B'),'P2' => array('D','E')); function permut($arr, $skip = array()){ if (count($arr) == 0) return array(''); $key = array_keys($arr); $a1 = $arr[$key[0]]; unset($arr[$key[0]]); if (!is_array($a1) or count($a1) == 0) $a1 = array(''); $out=array(); $tmp = permut($arr,$skip); $s = array_key_exists($key[0],$skip) ? $skip[$key[0]] : array(); foreach ($a1 as $val){ if (!in_array($val,$s)) foreach ($tmp as $val2) $out[] = $val.$val2; } return $out; } $b = permut($arrayin); print_r($b); $b = permut($arrayin,$skip); print_r($b); ?> The new and more difficult problem...Using the following array... [s1] => Array ( [0] => Array ( [id] => A, [limit] => Array ( [0] => S2, [1] => S3 ) ) [1] => Array ( [id] => B ) [2] => Array ( [id] => C, [limit] => S2G ) [3] => Array ( [id] => D, [limit] => S2E ) ) [s2] => Array ( [0] => Array ( [id] => E ) [1] => Array ( [id] => F, [limit] => Array ( [0] => S3A, [1] => S4 ) ) [2] => Array ( [id] => G, [limit] => S4 ) [3] => Array ( [id] => H, [limit] => S4O ) ) [s3] => Array ( [0] => Array ( [id] => A, [limit] => Array ( [0] => S4M, [1] => S4N ) ) [1] => Array ( [id] => B ) [2] => Array ( [id] => C, [limit] => S4O ) ) [s4] => Array( [0] => Array ( [id] => M ) [1] => Array ( [id] => N ) [2] => Array ( [id] => O ) ) I am attempting to calculate all the permutations enforcing certain rules along the way. EXAMPLE: In the S1 array... [0] "ID=A" has a "limit" array which should be "read" as; Calculate all possible permutations SKIPPING all values in the S2 and S3 arrays. [1] "ID=B" has no "limits" which should be "read" as; Calculate all possible permutations using S2,S3,S4 arrays. [2] "ID=C" has a "limit" which should be "read" as; Calculate all possible permutations using all the values contained in S2 SKIPPING ID=G, then continue normally thru S3,S4 arrays. [3] "ID=D" has a "limit" which should be "read" as; Calculate all possible permutations using all the values contained in S2 SKIPPING ID=E, then continue normally thru S3,S4 arrays. If the "limit" lists S2 then it implies you should SKIP the entire S2 array. If the "limit" lists S2+Value (ex: S2E) then it implies you should SKIP ONLY the value E within the S2 array. All listed "limit" rules should be enforced during the course of calculating the permuations. Finally, the number of possible values per array will flux as well as the number of arrays and the number of possible limits. I am only using a limited number of values and four arrays to make things simpler. Also I would like to define WHERE to start calculation from. So for example... $start = S2F OR $start = S3A or $start = S1A... simple no? Quote Link to comment Share on other sites More sharing options...
eviltwinkie Posted April 12, 2007 Author Share Posted April 12, 2007 Tricky...and yet no takers...haa Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.