評判が許されないとき
<?php
$array1=["joe","bob"];
$array2=["tom","bill"];
$array3=["dan","mary"];
$arr = array_merge($array1,$array2,$array3);
$result = array();
function noRepeatation($arr, $temp_string, &$collect) {
if ($temp_string != "")
$collect []= $temp_string;
for ($i=0; $i<sizeof($arr);$i++) {
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1); // removes and returns the i'th element
if (sizeof($arrcopy) > 0) {
noRepeatation($arrcopy, $temp_string ." " . $elem[0], $collect);
} else {
$collect []= $temp_string. " " . $elem[0];
}
}
}
$collect = array();
noRepeatation($arr, "", $collect);
print_r($collect);
?>
評判が許されるとき
<?php
$array1=["joe","bob"];
$array2=["tom","bill"];
$array3=["dan","mary"];
$arr = array_merge($array1,$array2,$array3);
$result = array();
function repeatation($arr, $level, &$result, $curr=array()) {
for($i = 0; $i < count($arr); $i++) {
$new = array_merge($curr, array($arr[$i]));
if($level == 1) {
sort($new);
if (!in_array($new, $result)) {
$result[] = $new;
}
} else {
repeatation($arr, $level - 1, $result, $new);
}
}
}
for ($i = 0; $i<count($arr); $i++) {
repeatation($arr, $i+1, $result);
}
foreach ($result as $arr) {
echo join(" ", $arr) . '<br>';
}
?>