Home > Backend Development > PHP Tutorial > PHP outputs permutations and combinations of multiple elements_PHP tutorial

PHP outputs permutations and combinations of multiple elements_PHP tutorial

WBOY
Release: 2016-07-13 17:54:33
Original
1088 people have browsed it

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           $s = $arr[$i];
           $ret = getCombinationToString(array_values($tmpArr), ($m-1), $result);
                                   
foreach($ret as $row) {
               $result[] = $s . $row;
         } 
}  
       
Return $result;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477961.htmlTechArticleSolve the problem: Take M elements out of an array containing N elements to form a new array. A total of possible combinations into an array and output [php] ?php $arr = array(a,b,c,d); $result = array();...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template