全排列php实现

function getRank($nums){
    $res = array();
    rank($nums,array(),$res);
    return $res;
}

function rank($nums,$tmp,&$res){
    if(count($tmp) == count($nums)){
        $res[] = $tmp;
    }else{
        foreach ($nums as $num) {
            if(!in_array($num,$tmp)){
                array_push($tmp,$num);
                rank($nums,$tmp,$res);
                array_pop($tmp);
            }
        }
    }
}

$result = getRank(array(1,2,3,4));
var_dump($result);

你可能感兴趣的:(全排列php实现)