Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中从数组中获取所有置换?_Php_Arrays_Permutation - Fatal编程技术网

如何在php中从数组中获取所有置换?

如何在php中从数组中获取所有置换?,php,arrays,permutation,Php,Arrays,Permutation,我有一个数组 $a = array(1,2,3,4,5); 我想获得数组中所有$n元素的组合 $n = 3 输出 1 2 3 1 3 4 1 4 5 2 3 5 2 4 5 . . . 5 1 2 基本上,您可以在数组中从头到尾地将当前数字放在开头,然后将数组的所有排列(开头没有数字)附加到数组中。如果使用递归,这相当简单。 例如: 现在调用生成所有置换的函数(此函数),并将得到的所有数组追加到该函数 每个函数调用所需的迭代次数是n(n)*(n-1)*(n-2).不久前,我对我的日常工

我有一个数组

$a = array(1,2,3,4,5);
我想获得数组中所有
$n
元素的组合

$n = 3
输出

1 2 3
1 3 4
1 4 5
2 3 5
2 4 5
.
.
.  
5 1 2

基本上,您可以在数组中从头到尾地将当前数字放在开头,然后将数组的所有排列(开头没有数字)附加到数组中。如果使用递归,这相当简单。 例如:

现在调用生成所有置换的函数(此函数),并将得到的所有数组追加到该函数


每个函数调用所需的迭代次数是
n
(n)*(n-1)*(n-2).

不久前,我对我的日常工作(不是程序员)有一个类似的问题。我找到了以下代码的javascript版本。希望我把它转码得足够好。我的评论。如果您可以等待一段时间(马上去度假),那么我可以解决如何限制递归调用以减少资源负担

<?php

function combinations($arr){
    $result = array();
    //the result array, returned by this outer function.
    function fn($active, $rest, &$a){
        if(!$active && !$rest)
            return;//If we have empty arrays, stoppit
        if(!$rest){
            //Are we out of remaining options? Yep, add the active array.
            $a[] = $active;
        }else{
            /*
              we are currently splitting the work between the two options. First is that we compute the 
              combinations of the currently $active and the $rest array offset by 1.
            */
            fn($active, array_slice($rest,1), $a);
            $active[] = $rest[0];
            //Next we add in the first element of the rest array to the active array, and slice off that new element to avoid duplicates.
            fn($active, array_slice($rest,1), $a);

        }
    } //Function that actually does the work;
    fn([],$arr,$result);
    return $result;
}

$combos = combinations([1,2,3,4,5]);
$combos = array_filter($combos,function($item){
    return count($item) == 2;
});

print_r($combos);

?>

这是我发现的第一段代码,它实际上是输出置换而不是组合。不过我有个问题。。。数字2、4、5、8、9、11、16、17、19和23来自哪里。可以改为0,1,2,3,4,5,6,7,8,9吗?我想知道如何在我正在开发的安卓应用程序中使用它。
<?php

function combinations($arr){
    $result = array();
    //the result array, returned by this outer function.
    function fn($active, $rest, &$a){
        if(!$active && !$rest)
            return;//If we have empty arrays, stoppit
        if(!$rest){
            //Are we out of remaining options? Yep, add the active array.
            $a[] = $active;
        }else{
            /*
              we are currently splitting the work between the two options. First is that we compute the 
              combinations of the currently $active and the $rest array offset by 1.
            */
            fn($active, array_slice($rest,1), $a);
            $active[] = $rest[0];
            //Next we add in the first element of the rest array to the active array, and slice off that new element to avoid duplicates.
            fn($active, array_slice($rest,1), $a);

        }
    } //Function that actually does the work;
    fn([],$arr,$result);
    return $result;
}

$combos = combinations([1,2,3,4,5]);
$combos = array_filter($combos,function($item){
    return count($item) == 2;
});

print_r($combos);

?>