Home >Backend Development >PHP Tutorial >PHP outputs permutations and combinations of multiple elements_PHP tutorial
Solve the problem: Find M elements from an array containing N elements to form a new array. A total of arrays that can be combined and output
[php]
$arr = array('a','b','c','d');
$result = array();
$t = getCombinationToString($arr, 4);
print_r($t);
function getCombinationToString($arr, $m) {
If ($m ==1) {
Return $arr;
}
$result = array();
$tmpArr = $arr;
Unset($tmpArr[0]);
for($i=0;$i
$ret = getCombinationToString(array_values($tmpArr), ($m-1), $result);
foreach($ret as $row) {
$result[] = $s . $row;
}
}
Return $result;
}
?>