简体   繁体   English

减少 PHP 阵列的分布

[英]Decreasing distribution for a PHP array

I'm having a problem in PHP, I want to get all possible combinations or distributions to add to some values, for example:我在 PHP 中遇到问题,我想获取所有可能的组合或分布以添加到某些值,例如:

$unities = 3;
$array = [ 1, 2, 3 , 4]

What I want to do is get every possibility to add that unities to the numbers in the array, like a loop that makes:我想要做的是获得将这些单位添加到数组中的数字的所有可能性,就像一个循环一样:

3 0 0 0
0 3 0 0
0 0 3 0
0 0 0 3
2 1 0 0
2 0 1 0
2 0 0 1
.......
.......
Continue
.......
.......

And add that to the array, so the result must be:并将其添加到数组中,因此结果必须是:

4 2 3 4
1 5 3 4
1 2 6 4
1 2 3 7
3 3 3 4
3 2 4 4
3 2 3 5
.......
.......
Continue
.......

I hope is well explained and you can help me.我希望得到很好的解释,你可以帮助我。

Thanks a lot.非常感谢。

If it's a fixed number then you can simply use nested for loops and an if statement to check that the sum is correct?如果它是一个固定数字,那么您可以简单地使用嵌套for循环和if语句来检查总和是否正确?

$array = [1,2,3,4];
$unities = 3;

for ($a = 0; $a <= $unities; $a++)
    for ($b = 0; $b <= $unities; $b++)
        for ($c = 0; $c <= $unities; $c++)
            for ($d = 0; $d <= $unities; $d++)
                if ($a + $b + $c + $d === $unities)
                    $output[] = [$a + $array[0], $b + $array[1], $c + $array[2], $d + $array[3]];

print_r($output);

Recursive Version递归版本

$array = [1,2,3,4];
$unities = 3;


function getPossibilities($target, $array, &$output, $arr = [])
{
    for ($i = 0; $i <= $target; $i++) {
        $currentArr = array_merge($arr, [$i]);
        if (count($array) === count($currentArr)) {
            if (array_sum($currentArr) === $target) {
                $tempOutput = [];
                for ($x = 0; $x < count($array); $x++) {
                    $tempOutput[] = $array[$x] + $currentArr[$x];
                }
                $output[] = $tempOutput;
            }
        } else {
            getPossibilities($target, $array, $output, $currentArr);
        }
    }
}

getPossibilities($unities, $array, $output);

print_r($output);

I got this function that returns the permutation of the array passed into it from this answer https://stackoverflow.com/a/13194803/15350139 .我得到了这个 function ,它返回从这个答案https://stackoverflow.com/a/13194803/15350139传递给它的数组的排列。 From O'Reilly's "PHP Cookbook".来自 O'Reilly 的“PHP Cookbook”。

function pc_permute($items, $perms = [])
{
    if (empty($items)) {
        $return = [$perms];
    } else {
        $return = [];

        for ($i = count($items) - 1; $i >= 0; --$i) {
            $new_items = $items;
            $new_perms = $perms;

            list($foo) = array_splice($new_items, $i, 1);
            array_unshift($new_perms, $foo);

            $return = array_merge($return, pc_permute($new_items, $new_perms));
        }
    }

    return $return;
}

Then from the permutations of the required array, I use this add the $unities to each element然后从所需数组的排列中,我使用$unities添加到每个元素

function decreasingDist($unities, $array)
{
    foreach ($array as $i => $num) $array[$i] += $num;
    return $array;
}

Then this will return the desired result然后这将返回所需的结果

$unities = 3;
$array = [ 1, 2, 3 , 4];

decreasingDist($untities, pc_permute($array));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM