Jump to content

eviltwinkie

New Members
  • Posts

    8
  • Joined

  • Last visited

    Never

Everything posted by eviltwinkie

  1. 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?
  2. once again...sasa...your the man... simple...clean...elegant... i like...
  3. I started off just trying to figure out how best to perform a nested foreach problem...I already had a static solution, but it simply was not elegant. I always try to make everything as dynamic as possible. So while originally a self-learning thing...I actually implemented the code snippent and it works very well which has now got me to thinking about growing it more. I included all the information that was required at the time and got some great help. How to calculate all the permutations using different tables as listed above. I am now attempting to modify the code to ALSO filter out certain values. Example...Using the same array...I want to compute all the valid permutations except for "B"...I want to "skip" all possible combinations for "E" and "F" but still calculate all the valid ones using "D". Array ( [P1] => Array ( 0 => A 1 => B 2 => C ) [P2] => Array ( 0 => D 1 => E 2 => F ) [P3] => Array ( 0 => G ) [P4] => Array ( 0 => H 1 => I ) ) Why you ask? First because I think I could actually use this in my current code to further simply things...and secondly...because its a challenge and I want to know...reminds me...should I mark the topic as solved as if I wanted help on this second part would constitute a new thread? or should it just be left as is? Thanks!
  4. Not really what I am looking for. I am trying to calculate all the possible permutations for all the values in the arrays. For example... ADGH ADGI AEGH AEGI AFGH AFGI etc etc Do you know of a better way?
  5. Array ( [P1] => Array ( 0 => A 1 => B 2 => C ) [P2] => Array ( 0 => D 1 => E 2 => F ) [P3] => Array ( 0 => G ) [P4] => Array ( 0 => H 1 => I ) ) foreach ($arrayin[P1] as $value1) { foreach ($arrayin[P2] as $value2) { foreach ($arrayin[P3] as $value3) { foreach ($arrayin[P4] as $value4) { $arrayout[] = "$value1$value2$value3$value4"; } } } } $arrayout has all the proper possible permutations of the above arrays...no problem there...here's where it gets tricky... If I want to have P5 or P6...etc...how do you create a function or chunk of code such that you can... count(array) and nest each foreach such that you get the same results without having to statically write a case for each senario? Thanks!
  6. Very simple question which either I am smoking crack and therefore cannot see the answer...or is just impossible to do. Using PHP5... $array = [0] => Array ( [Misc] => Blah ) [1] => Array ( [Animals] => Moo ) [2] => Array ( [Animals] => Oink ) [3] => Array ( [Stuff] => Thing1 ) [4] => Array ( [Stuff] => Thing2 ) All I need to accomplish is to.... $result = array_merge_recursive($array['0'],$array['1'],$array['2'],$array['3'],$array['4']); Which yields... $result = [Misc] => Blah [Animals] => Array ( [0] => Moo [1] => Oink ) [Stuff] => Array ( [0] => Thing1 [1] => Thing2 ) no problem there...however I do NOT know how many arrays I will need to list within the array_merge_recursive() function...so I tried to do a count and create a variable with the array broken into the proper format with the "," between them like... $variable = $array['0'],$array['1'],$array['2'],$array['3'],$array['4']; however array_merge_recursive($variable) refuses to work... soooooo...the million dollar question is.... how can I either load up a variable properly formatted which the array_merge_recursive function will accept? OR how can I do an array_merge_recursive($array['0'],$array['1],etc,etc,etc, until EOF??) Any takers??
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.