PHP实现数组元素排列组合


$arr = array(
        "康师傅, 康sir",
        "方便面 矿泉水 ",
        "新闻"
    );

$res = array();

while($s = array_shift($arr)){
    if(!empty($s)){
        $s = str_replace(",", ",", $s);
        //echo $s."\n";
        $leaves = preg_split("/[\,\s+]/", $s,-1,PREG_SPLIT_NO_EMPTY);
        $ret = array();

        if($res){
            foreach($res as $_leaf){
                foreach($leaves as $leaf){
                    $ret[] = $_leaf." ".$leaf;
                }
            }
        }else{
            foreach($leaves as $leaf){
                $ret[] = $leaf;
            }
        }

        //print_r($ret);
        $res = $ret;
    }
}

print_r($res);

// 打印结果
Array
(
    [0] => 康师傅 方便面 新闻
    [1] => 康师傅 矿泉水 新闻
    [2] => 康sir 方便面 新闻
    [3] => 康sir 矿泉水 新闻
)