Home > php教程 > php手册 > PHP输出多个元素的排列组合

PHP输出多个元素的排列组合

WBOY
Release: 2016-06-13 10:52:32
Original
1423 people have browsed it

解决问题:求一个含有N个元素的数组中取出M个元素组成新的数组,一共可以组合成的数组并输出
[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; 

?> 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template