Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
80 views
in Technique[技术] by (71.8m points)

PHP Get all possible percentage options into array

What I am trying to do is get all possible percentage value combinations for a given set of keys in an array. Here is what I have so far, which is supposed to do all possible combinations up to 5:

<?php

namespace AppHttpControllers;

class CalculateController extends Controller
{

    protected $percentages = [];

    protected $maxPercent = 5;

    public function index()
    {
        $keys = ['key1' => 0, 'key2' => 0, 'key4' => 0, 'key4' => 0];
        $percentKeys = [];

        $arrayKeys = array_keys($keys);

        foreach($keys as $key => $value) {

            $keyPlacement = array_search($key, array_keys($keys));

                while($keyPlacement >= 0){
                    $keys[$arrayKeys[$keyPlacement]] = 0;
                    $keys = $this->loop($keys, $arrayKeys[$keyPlacement]);
                $keyPlacement = $keyPlacement - 1;
                }
        }

        $this->percentages = array_unique($this->percentages, SORT_REGULAR);
        print_r($this->percentages);

    }

    public function loop( $array, $key ){
        for($i = 0 ; $i < $this->maxPercent + 1 ; $i++) {
            $array[$key] = $i;
            $this->percentages[] = $array;
        }
        return $array;
    }

}

This is outputting:

Array
(
[0] => Array
    (
        [key1] => 0
        [key2] => 0
        [key3] => 0
        [key4] => 0
    )

[1] => Array
    (
        [key1] => 1
        [key2] => 0
        [key3] => 0
        [key4] => 0
    )

[2] => Array
    (
        [key1] => 2
        [key2] => 0
        [key3] => 0
        [key4] => 0
    )

[3] => Array
    (
        [key1] => 3
        [key2] => 0
        [key3] => 0
        [key4] => 0
    )

[4] => Array
    (
        [key1] => 4
        [key2] => 0
        [key3] => 0
        [key4] => 0
    )

[5] => Array
    (
        [key1] => 5
        [key2] => 0
        [key3] => 0
        [key4] => 0
    )

[7] => Array
    (
        [key1] => 5
        [key2] => 1
        [key3] => 0
        [key4] => 0
    )

[8] => Array
    (
        [key1] => 5
        [key2] => 2
        [key3] => 0
        [key4] => 0
    )

[9] => Array
    (
        [key1] => 5
        [key2] => 3
        [key3] => 0
        [key4] => 0
    )

[10] => Array
    (
        [key1] => 5
        [key2] => 4
        [key3] => 0
        [key4] => 0
    )

[11] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 0
        [key4] => 0
    )

[12] => Array
    (
        [key1] => 0
        [key2] => 5
        [key3] => 0
        [key4] => 0
    )

[13] => Array
    (
        [key1] => 1
        [key2] => 5
        [key3] => 0
        [key4] => 0
    )

[14] => Array
    (
        [key1] => 2
        [key2] => 5
        [key3] => 0
        [key4] => 0
    )

[15] => Array
    (
        [key1] => 3
        [key2] => 5
        [key3] => 0
        [key4] => 0
    )

[16] => Array
    (
        [key1] => 4
        [key2] => 5
        [key3] => 0
        [key4] => 0
    )

[19] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 1
        [key4] => 0
    )

[20] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 2
        [key4] => 0
    )

[21] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 3
        [key4] => 0
    )

[22] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 4
        [key4] => 0
    )

[23] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 5
        [key4] => 0
    )

[24] => Array
    (
        [key1] => 5
        [key2] => 0
        [key3] => 5
        [key4] => 0
    )

[25] => Array
    (
        [key1] => 5
        [key2] => 1
        [key3] => 5
        [key4] => 0
    )

[26] => Array
    (
        [key1] => 5
        [key2] => 2
        [key3] => 5
        [key4] => 0
    )

[27] => Array
    (
        [key1] => 5
        [key2] => 3
        [key3] => 5
        [key4] => 0
    )

[28] => Array
    (
        [key1] => 5
        [key2] => 4
        [key3] => 5
        [key4] => 0
    )

[30] => Array
    (
        [key1] => 0
        [key2] => 5
        [key3] => 5
        [key4] => 0
    )

[31] => Array
    (
        [key1] => 1
        [key2] => 5
        [key3] => 5
        [key4] => 0
    )

[32] => Array
    (
        [key1] => 2
        [key2] => 5
        [key3] => 5
        [key4] => 0
    )

[33] => Array
    (
        [key1] => 3
        [key2] => 5
        [key3] => 5
        [key4] => 0
    )

[34] => Array
    (
        [key1] => 4
        [key2] => 5
        [key3] => 5
        [key4] => 0
    )

[37] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 5
        [key4] => 1
    )

[38] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 5
        [key4] => 2
    )

[39] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 5
        [key4] => 3
    )

[40] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 5
        [key4] => 4
    )

[41] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 5
        [key4] => 5
    )

[42] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 0
        [key4] => 5
    )

[43] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 1
        [key4] => 5
    )

[44] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 2
        [key4] => 5
    )

[45] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 3
        [key4] => 5
    )

[46] => Array
    (
        [key1] => 5
        [key2] => 5
        [key3] => 4
        [key4] => 5
    )

[48] => Array
    (
        [key1] => 5
        [key2] => 0
        [key3] => 5
        [key4] => 5
    )

[49] => Array
    (
        [key1] => 5
        [key2] => 1
        [key3] => 5
        [key4] => 5
    )

[50] => Array
    (
        [key1] => 5
        [key2] => 2
        [key3] => 5
        [key4] => 5
    )

[51] => Array
    (
        [key1] => 5
        [key2] => 3
        [key3] => 5
        [key4] => 5
    )

[52] => Array
    (
        [key1] => 5
        [key2] => 4
        [key3] => 5
        [key4] => 5
    )

[54] => Array
    (
        [key1] => 0
        [key2] => 5
        [key3] => 5
        [key4] => 5
    )

[55] => Array
    (
        [key1] => 1
        [key2] => 5
        [key3] => 5
        [key4] => 5
    )

[56] => Array
    (
        [key1] => 2
        [key2] => 5
        [key3] => 5
        [key4] => 5
    )

[57] => Array
    (
        [key1] => 3
        [key2] => 5
        [key3] => 5
        [key4] => 5
    )

[58] => Array
    (
        [key1] => 4
        [key2] => 5
        [key3] => 5
        [key4] => 5
    )

)

So I'm close but not quite there. Any feedback is appreciated.

question from:https://stackoverflow.com/questions/65853046/php-get-all-possible-percentage-options-into-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand the question, you have a max percentage (in your case 5) and a certain number of keys (in this case 1...4). You want to populate these four keys with all possible values of all integer percentages, zero included, up to the max included, with repetitions allowed.

So in this case from 0 0 0 0 up to 5 5 5 5.

In this case, you can have at the most pow($MaxPercentage+1, $keyNumber) values in your result. Given any number from 0 to this maximum number, its combination will be

function nthCombo($idx, $keyNumber, $top) {
    $result = array_fill(0, $keyNumber, 0);
    $pos = 0;
    while ($idx) {
        $result[$pos++] = $idx % $top;
        $idx = floor($idx / $top);
    }
    return $result;
}

So if you want all the percentages, all together:

$maxPercentage = 5;
$keyNumber = 4;
$top = $maxPercentage + 1;

$combinations = pow($top, $keyNumber);

print $combinations . "
";
for ($i = 0; $i < $combinations; $i++) {
    $allcombi[] = nthCombo($i, $keyNumber, $top);
}

This also allows you to work without the need of actually storing the result, you can process it one value at a time.

(The mapping to named keys is immediate using array_combine())

specific percentages or strings

If you have a given number of percentages but they aren't in any simple progression (for example 11%, 13%, 22%, 22.5%, 27%, 31%), or maybe they are strings like [ 'foo', 'bar', 'baz' ], you can still use the approach above. You just use a map:

$percs = [ 11%, 13%, 22%, 22.5%, 27%, 31% ];

Now when the algorithm gives you $combo = [ 0, 2, 1, 5 ] you translate every number, e.g. 2, into $percs[2], which is 22%:

$perc2 = array_map(
    function($idx) use ($percs) { 
        return $percs[$idx]; 
    }, 
    $combo
);

and obtain $perc2 = [ 11%, 22%, 13%, 31% ].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...