title: PHP Get all possible percentage options into array
body: <p>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:</p>
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  class CalculateController extends Controller
  {
  
      protected $percentages = [];
  
      protected $maxPercent = 5;
  
      public function index()
      {
          $keys = ['key1' =&gt; 0, 'key2' =&gt; 0, 'key4' =&gt; 0, 'key4' =&gt; 0];
          $percentKeys = [];
  
          $arrayKeys = array_keys($keys);
  
          foreach($keys as $key =&gt; $value) {
  
              $keyPlacement = array_search($key, array_keys($keys));
  
                  while($keyPlacement &gt;= 0){
                      $keys[$arrayKeys[$keyPlacement]] = 0;
                      $keys = $this-&gt;loop($keys, $arrayKeys[$keyPlacement]);
                  $keyPlacement = $keyPlacement - 1;
                  }
          }
  
          $this-&gt;percentages = array_unique($this-&gt;percentages, SORT_REGULAR);
          print_r($this-&gt;percentages);
  
      }
  
      public function loop( $array, $key ){
          for($i = 0 ; $i &lt; $this-&gt;maxPercent + 1 ; $i++) {
              $array[$key] = $i;
              $this-&gt;percentages[] = $array;
          }
          return $array;
      }
  
  }
  </code></pre>
  <p>This is outputting:</p>
  <pre><code>Array
  (
  [0] =&gt; Array
      (
          [key1] =&gt; 0
          [key2] =&gt; 0
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [1] =&gt; Array
      (
          [key1] =&gt; 1
          [key2] =&gt; 0
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [2] =&gt; Array
      (
          [key1] =&gt; 2
          [key2] =&gt; 0
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [3] =&gt; Array
      (
          [key1] =&gt; 3
          [key2] =&gt; 0
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [4] =&gt; Array
      (
          [key1] =&gt; 4
          [key2] =&gt; 0
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [5] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 0
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [7] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 1
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [8] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 2
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [9] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 3
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [10] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 4
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [11] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [12] =&gt; Array
      (
          [key1] =&gt; 0
          [key2] =&gt; 5
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [13] =&gt; Array
      (
          [key1] =&gt; 1
          [key2] =&gt; 5
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [14] =&gt; Array
      (
          [key1] =&gt; 2
          [key2] =&gt; 5
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [15] =&gt; Array
      (
          [key1] =&gt; 3
          [key2] =&gt; 5
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [16] =&gt; Array
      (
          [key1] =&gt; 4
          [key2] =&gt; 5
          [key3] =&gt; 0
          [key4] =&gt; 0
      )
  
  [19] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 1
          [key4] =&gt; 0
      )
  
  [20] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 2
          [key4] =&gt; 0
      )
  
  [21] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 3
          [key4] =&gt; 0
      )
  
  [22] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 4
          [key4] =&gt; 0
      )
  
  [23] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 0
      )
  
  [24] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 0
          [key3] =&gt; 5
          [key4] =&gt; 0
      )
  
  [25] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 1
          [key3] =&gt; 5
          [key4] =&gt; 0
      )
  
  [26] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 2
          [key3] =&gt; 5
          [key4] =&gt; 0
      )
  
  [27] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 3
          [key3] =&gt; 5
          [key4] =&gt; 0
      )
  
  [28] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 4
          [key3] =&gt; 5
          [key4] =&gt; 0
      )
  
  [30] =&gt; Array
      (
          [key1] =&gt; 0
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 0
      )
  
  [31] =&gt; Array
      (
          [key1] =&gt; 1
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 0
      )
  
  [32] =&gt; Array
      (
          [key1] =&gt; 2
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 0
      )
  
  [33] =&gt; Array
      (
          [key1] =&gt; 3
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 0
      )
  
  [34] =&gt; Array
      (
          [key1] =&gt; 4
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 0
      )
  
  [37] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 1
      )
  
  [38] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 2
      )
  
  [39] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 3
      )
  
  [40] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 4
      )
  
  [41] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 5
      )
  
  [42] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 0
          [key4] =&gt; 5
      )
  
  [43] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 1
          [key4] =&gt; 5
      )
  
  [44] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 2
          [key4] =&gt; 5
      )
  
  [45] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 3
          [key4] =&gt; 5
      )
  
  [46] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 5
          [key3] =&gt; 4
          [key4] =&gt; 5
      )
  
  [48] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 0
          [key3] =&gt; 5
          [key4] =&gt; 5
      )
  
  [49] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 1
          [key3] =&gt; 5
          [key4] =&gt; 5
      )
  
  [50] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 2
          [key3] =&gt; 5
          [key4] =&gt; 5
      )
  
  [51] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 3
          [key3] =&gt; 5
          [key4] =&gt; 5
      )
  
  [52] =&gt; Array
      (
          [key1] =&gt; 5
          [key2] =&gt; 4
          [key3] =&gt; 5
          [key4] =&gt; 5
      )
  
  [54] =&gt; Array
      (
          [key1] =&gt; 0
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 5
      )
  
  [55] =&gt; Array
      (
          [key1] =&gt; 1
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 5
      )
  
  [56] =&gt; Array
      (
          [key1] =&gt; 2
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 5
      )
  
  [57] =&gt; Array
      (
          [key1] =&gt; 3
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 5
      )
  
  [58] =&gt; Array
      (
          [key1] =&gt; 4
          [key2] =&gt; 5
          [key3] =&gt; 5
          [key4] =&gt; 5
      )
  
  )
  </code></pre>
  <p>So I'm close but not quite there. Any feedback is appreciated.</p>