Demand:
There is an array [' A ', ' B ', ' C ', ' CD ']
You need to remove any m non-repeating elements from the array, listing all the possibilities (and not requiring duplicates such as [' A ', ' B ', ' C ') and [' A ', ' C ', ' B ']).
You can use the recursive return to complete:
<?phpfunction getcombinationtostring ($arr, $m) {if ($m = = 1) { return $arr;} $result = []; $tmpArr = $arr; unset ($tmpArr [0]); for ($i = 0; $i < count ($arr); $i + +) {$s = $arr [$i]; $ret = Getcombinationt Ostring (Array_values ($TMPARR), ($m-1), $result), foreach ($ret as $row) {$val = $s. ‘,‘ . $row, $val = Explode (', ', $val), sort ($val), $val = Implode (', ', $val), if (! In_array ($s, explode (', ', $row)) && In_ Array ($val, $result)) {$result [] = $val;}}} return $result;} $arr = [' A ', ' B ', ' C ', ' CD ']; $t = getcombinationtostring ($arr, 3); Echo ' <pre> ';p rint_r ($t);
If you allow repeating combinations, you can annotate the conditions of the line:17.
Output:
Array ( [0] = A,b,c [1] = A,B,CD [2] = = A,C,CD [3] = = B,C,CD)
PHP implementation Math problem: combination