3v4l.org

run code in 300+ PHP versions simultaneously
<?php $percent = [20.88, 14.93, 14.14, 13.29, 5.06, 4.43, 4.24, 4.22, 2.57, 2.51, 2.38, 2.18, 1.94, 1.80, 1.34, 1.21, 0.81, 0.63, 0.50, 0.48, 0.30, 0.16]; $limit = 6; if ($limit * count($percent) < 100) die("Not possible"); $limited = array_map(function($value) use ($limit) { return min($value, $limit); }, $percent); while (($remain = 100 - array_sum($limited)) > 0) { $filtered = array_filter($limited, function($v) use ($limit) { return $v < $limit; }); $evenly = $remain / count($filtered); $limited = array_map(function($value) use ($evenly, $limit) { return min($value + $evenly, $limit); }, $limited); } print_r($limited); print_r(array_sum($limited));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 15
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 15
Branch analysis from position: 40
Branch analysis from position: 15
filename:       /in/Ms9Vt
function name:  (null)
number of ops:  50
compiled vars:  !0 = $percent, !1 = $limit, !2 = $limited, !3 = $filtered, !4 = $evenly, !5 = $remain
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    5     1        ASSIGN                                                   !1, 6
    6     2        COUNT                                            ~8      !0
          3        MUL                                              ~9      !1, ~8
          4        IS_SMALLER                                               ~9, 100
          5      > JMPZ                                                     ~10, ->7
          6    > > EXIT                                                     'Not+possible'
    8     7    >   INIT_FCALL                                               'array_map'
          8        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FMs9Vt%3A8%240'
          9        BIND_LEXICAL                                             ~11, !1
         10        SEND_VAL                                                 ~11
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $12     
         13        ASSIGN                                                   !2, $12
    9    14      > JMP                                                      ->33
   10    15    >   INIT_FCALL                                               'array_filter'
         16        SEND_VAR                                                 !2
         17        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FMs9Vt%3A10%241'
         18        BIND_LEXICAL                                             ~14, !1
         19        SEND_VAL                                                 ~14
         20        DO_ICALL                                         $15     
         21        ASSIGN                                                   !3, $15
   11    22        COUNT                                            ~17     !3
         23        DIV                                              ~18     !5, ~17
         24        ASSIGN                                                   !4, ~18
   12    25        INIT_FCALL                                               'array_map'
         26        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FMs9Vt%3A12%242'
         27        BIND_LEXICAL                                             ~20, !4
         28        BIND_LEXICAL                                             ~20, !1
         29        SEND_VAL                                                 ~20
         30        SEND_VAR                                                 !2
         31        DO_ICALL                                         $21     
         32        ASSIGN                                                   !2, $21
    9    33    >   INIT_FCALL                                               'array_sum'
         34        SEND_VAR                                                 !2
         35        DO_ICALL                                         $23     
         36        SUB                                              ~24     100, $23
         37        ASSIGN                                           ~25     !5, ~24
         38        IS_SMALLER                                               0, ~25
         39      > JMPNZ                                                    ~26, ->15
   15    40    >   INIT_FCALL                                               'print_r'
         41        SEND_VAR                                                 !2
         42        DO_ICALL                                                 
   16    43        INIT_FCALL                                               'print_r'
         44        INIT_FCALL                                               'array_sum'
         45        SEND_VAR                                                 !2
         46        DO_ICALL                                         $28     
         47        SEND_VAR                                                 $28
         48        DO_ICALL                                                 
         49      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FMs9Vt%3A8%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Ms9Vt
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $value, !1 = $limit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        INIT_FCALL                                               'min'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $2      
          6      > RETURN                                                   $2
          7*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FMs9Vt%3A8%240

Function %00%7Bclosure%7D%2Fin%2FMs9Vt%3A10%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Ms9Vt
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $v, !1 = $limit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        IS_SMALLER                                       ~2      !0, !1
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FMs9Vt%3A10%241

Function %00%7Bclosure%7D%2Fin%2FMs9Vt%3A12%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Ms9Vt
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $value, !1 = $evenly, !2 = $limit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
          3        INIT_FCALL                                               'min'
          4        ADD                                              ~3      !0, !1
          5        SEND_VAL                                                 ~3
          6        SEND_VAR                                                 !2
          7        DO_ICALL                                         $4      
          8      > RETURN                                                   $4
          9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FMs9Vt%3A12%242

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
179.84 ms | 1404 KiB | 23 Q