RE: [PHP] Generate every possible combination

From: Date: Thu, 16 May 2002 04:27:19 +0000
Subject: RE: [PHP] Generate every possible combination
Groups: php.general 
Request: Send a blank email to php-general+get-97869@lists.php.net to get a copy of this message
recursive function, passing back in the array, minus the current position and current combination. when array is empty, use current combination. um... something like (but not tested) function comby($arr, $comb = "") { $num = count($arr); if ($num == 0) { echo $comb; return; } for ($i = 0; $i < $num; $i++) { $tmp_comb = $comb . $arr[$i]; $tmp_arr = $arr; array_splice($arr, $i, 1); comby($tmp_arr, $comb); } } $array = Array("A", "B", "C"); comby($array); -----Original Message----- From: Evan Nemerson [mailto:evan@coeus-group.com] Sent: Thursday, May 16, 2002 2:17 PM To: php-general@lists.php.net Subject: [PHP] Generate every possible combination I need to generate every possible combination of the the values in an array. For example, if... $array = Array("A", "B", "C"); I want to be able to do something like print_r(magic_function($array)); which would output Array ( [0] => "ABC" [1] => "ACB" [2] => "BAC" [3] => "BCA" [4] => "CAB" [5] => "CBA" ) I really have no idea where to begin. The best lead I can think of is that there are going to be n! elements in the output array, where n is the size of the input array. Any help would be greatly appreciated. Evan -- Think not those faithful who praise all thy words and actions, but those who kindly reprove thy faults. Socrates -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.general (#97869) next »