Home  >  Article  >  Backend Development  >  PHP permutations

PHP permutations

巴扎黑
巴扎黑Original
2016-11-23 13:12:241107browse

<?php
$arr = array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;);
combine_array($arr);
function combine_array($arr, $index=0) {
static $num = 0;
$arr_len = count($arr);
if($arr_len == $index) {
++$num;
echo $num.&#39; &#39;. join(",", $arr) . &#39;<br/>&#39;;
} else {
for($i=$index; $i<$arr_len; $i++) {
list($arr[$index], $arr[$i]) = array($arr[$i], $arr[$index]);
combine_array($arr, $index+1);
list($arr[$index], $arr[$i]) = array($arr[$i], $arr[$index]);
}
}
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn