note 59113 deleted from control-structures.for by felipe

From: Date: Tue, 29 Jan 2008 11:21:13 +0000
Subject: note 59113 deleted from control-structures.for by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-135515@lists.php.net to get a copy of this message
Note Submitter: see_the_address at the_message dot text ;) ---- Here a bit more flexible solution of a task "brutforce" by "jdtoth -at- gmail dot com", and also should be faster ;-) It uses recursion. But it requires quite much memory (especially when playing with $num_positions), I'm not sure it's OK... If anybody has ideas about that - you are welcome: ultraBoy aat mail ddot ru <?php function get_result($len, &$alphabet) { if ($len == 1) { $result = array(); while (list($row_key, $row) = each($alphabet)) { $result[] = array($row); }; } else { $result = get_result($len - 1, $alphabet); reset($result); $new_result = array(); while (list($row_key, $row) = each($result)) { reset($alphabet); while (list($l_key, $l) = each($alphabet)) { $new_row = $row; $new_row[] = $l; $new_result[] = $new_row; }; } $result = $new_result; } return $result; } // suspected combination numbers $numbers = array(12,18,24,36); // how many numbers are in one combunation $num_positions = 3; echo 'Total rows: '.pow(count($numbers), $num_positions).'<br />'; $result = get_result($num_positions, $numbers); foreach ($result as $row_num => $row) { echo "$row_num: "; foreach ($row as $val) { echo $val.' '; } echo '<br />'; } ?>

« previous php.notes (#135515) next »