function generate_gives_to($group_list, $attempt = 0) { $recipients = []; $givesto = []; $attempt += 1; foreach ($group_list as $k1 => $group) { $possible_recipients = array_merge(array_slice($group_list, 0, $k1), array_slice($group_list, $k1 + 1)); $possible_recipients = array_diff(flatten($possible_recipients), $recipients); foreach ($group as $k2 => $person) { if (empty($possible_recipients)) { if ($attempt < 10) { return generate_gives_to($group_list, $attempt); } else { echo " - INCOMPLETE SOLUTION - "; } } $k = array_rand($possible_recipients); $givesto[$person] = $possible_recipients[$k]; $recipients[] = $possible_recipients[$k]; unset($possible_recipients[$k]); } } fancy_print($givesto); return $givesto; }
function generate_gives_to($group_list, $fancy_print = true, $attempt = 0) { $recipients = []; $givesto = []; $attempt += 1; foreach ($group_list as $k1 => $group) { $possible_recipients = array_merge(array_slice($group_list, 0, $k1), array_slice($group_list, $k1 + 1)); $possible_recipients = array_diff(flatten($possible_recipients), $recipients); foreach ($group as $k2 => $person) { if (empty($possible_recipients)) { if ($attempt < 10) { return generate_gives_to($group_list, $fancy_print, $attempt); } else { return false; } } $k = array_rand($possible_recipients); $givesto[trim($person)] = trim($possible_recipients[$k]); $recipients[] = $possible_recipients[$k]; unset($possible_recipients[$k]); } } //echo fancy_print($givesto); //return $givesto; return $fancy_print ? fancy_print($givesto) : json_encode($givesto); }