3v4l.org

run code in 300+ PHP versions simultaneously
<? $expectedAmount = 13460.39; $arr = array(1090, 800, 293, 456.35,483.6,5766,1295,424,2005.15,944.5,7100,6800,7100); // Only modify above lines function printCombination($arr, $n, $r, &$result) { $data = array(); // int data[r]; combinationUtil($arr, $data, 0, $n-1, 0 ,$r, $result); } function combinationUtil($arr, $data, $start, $end, $index, $r, &$result) { if ($index == $r) { $tmpArray = array(); for ($i = 0; $i < $r; $i++) { array_push($tmpArray, $data[$i]); // echo $data[$i]; } // echo "<br>"; array_push($result, $tmpArray); return; } for ($i = $start; $i <= $end && $end - $i +1 >= $r - $index; $i++) { $data[$index] = $arr[$i]; combinationUtil($arr, $data, $i+1, $end, $index+1, $r, $result); } } function printValue($arr) { $string = ""; foreach ($arr as $value) { $string .= "$".$value.", "; } echo substr($string, 0, strlen($string)-2); } $result = array(); $n = sizeof($arr)/sizeof($arr[0]); for ($i = 1; $i <= $n; $i++) { printCombination($arr, $n, $i, $result); } $highestAmount = 0; $highestCombination = NULL; foreach ($result as $value) { $tempAmount = 0; foreach ($value as $cost) { $tempAmount += $cost; } if ($tempAmount > $highestAmount && $expectedAmount > $tempAmount) { $highestAmount = $tempAmount; $highestCombination = $value; } } echo "highestAmount: ". $highestAmount."\n"; echo "highestCombination: "; printValue($highestCombination); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 10
Branch analysis from position: 19
2 jumps found. (Code = 77) Position 1 = 22, Position 2 = 37
Branch analysis from position: 22
2 jumps found. (Code = 78) Position 1 = 23, Position 2 = 37
Branch analysis from position: 23
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 28
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 28
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 28
2 jumps found. (Code = 46) Position 1 = 31, Position 2 = 33
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 36
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 36
Branch analysis from position: 33
Branch analysis from position: 28
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 10
Branch analysis from position: 19
Branch analysis from position: 10
filename:       /in/KimaE
function name:  (null)
number of ops:  46
compiled vars:  !0 = $expectedAmount, !1 = $arr, !2 = $result, !3 = $n, !4 = $i, !5 = $highestAmount, !6 = $highestCombination, !7 = $value, !8 = $tempAmount, !9 = $cost
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, 13460.4
    3     1        ASSIGN                                                   !1, <array>
   38     2        ASSIGN                                                   !2, <array>
   39     3        COUNT                                            ~13     !1
          4        FETCH_DIM_R                                      ~14     !1, 0
          5        COUNT                                            ~15     ~14
          6        DIV                                              ~16     ~13, ~15
          7        ASSIGN                                                   !3, ~16
   40     8        ASSIGN                                                   !4, 1
          9      > JMP                                                      ->17
   41    10    >   INIT_FCALL                                               'printcombination'
         11        SEND_VAR                                                 !1
         12        SEND_VAR                                                 !3
         13        SEND_VAR                                                 !4
         14        SEND_REF                                                 !2
         15        DO_FCALL                                      0          
   40    16        PRE_INC                                                  !4
         17    >   IS_SMALLER_OR_EQUAL                                      !4, !3
         18      > JMPNZ                                                    ~21, ->10
   44    19    >   ASSIGN                                                   !5, 0
   45    20        ASSIGN                                                   !6, null
   46    21      > FE_RESET_R                                       $24     !2, ->37
         22    > > FE_FETCH_R                                               $24, !7, ->37
   47    23    >   ASSIGN                                                   !8, 0
   48    24      > FE_RESET_R                                       $26     !7, ->28
         25    > > FE_FETCH_R                                               $26, !9, ->28
   49    26    >   ASSIGN_OP                                     1          !8, !9
   48    27      > JMP                                                      ->25
         28    >   FE_FREE                                                  $26
   51    29        IS_SMALLER                                       ~28     !5, !8
         30      > JMPZ_EX                                          ~28     ~28, ->33
         31    >   IS_SMALLER                                       ~29     !8, !0
         32        BOOL                                             ~28     ~29
         33    > > JMPZ                                                     ~28, ->36
   52    34    >   ASSIGN                                                   !5, !8
   53    35        ASSIGN                                                   !6, !7
   46    36    > > JMP                                                      ->22
         37    >   FE_FREE                                                  $24
   56    38        CONCAT                                           ~32     'highestAmount%3A+', !5
         39        CONCAT                                           ~33     ~32, '%0A'
         40        ECHO                                                     ~33
   57    41        ECHO                                                     'highestCombination%3A+'
   58    42        INIT_FCALL                                               'printvalue'
         43        SEND_VAR                                                 !6
         44        DO_FCALL                                      0          
   60    45      > RETURN                                                   1

Function printcombination:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KimaE
function name:  printCombination
number of ops:  16
compiled vars:  !0 = $arr, !1 = $n, !2 = $r, !3 = $result, !4 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
    8     4        ASSIGN                                                   !4, <array>
    9     5        INIT_FCALL_BY_NAME                                       'combinationUtil'
          6        SEND_VAR_EX                                              !0
          7        SEND_VAR_EX                                              !4
          8        SEND_VAL_EX                                              0
          9        SUB                                              ~6      !1, 1
         10        SEND_VAL_EX                                              ~6
         11        SEND_VAL_EX                                              0
         12        SEND_VAR_EX                                              !2
         13        SEND_VAR_EX                                              !3
         14        DO_FCALL                                      0          
   10    15      > RETURN                                                   null

End of function printcombination

Function combinationutil:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 25
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 12
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 12
Branch analysis from position: 20
Branch analysis from position: 12
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
2 jumps found. (Code = 46) Position 1 = 44, Position 2 = 49
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 27
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 46) Position 1 = 44, Position 2 = 49
Branch analysis from position: 44
Branch analysis from position: 49
Branch analysis from position: 49
filename:       /in/KimaE
function name:  combinationUtil
number of ops:  51
compiled vars:  !0 = $arr, !1 = $data, !2 = $start, !3 = $end, !4 = $index, !5 = $r, !6 = $result, !7 = $tmpArray, !8 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
          4        RECV                                             !4      
          5        RECV                                             !5      
          6        RECV                                             !6      
   13     7        IS_EQUAL                                                 !4, !5
          8      > JMPZ                                                     ~9, ->25
   14     9    >   ASSIGN                                                   !7, <array>
   15    10        ASSIGN                                                   !8, 0
         11      > JMP                                                      ->18
   16    12    >   INIT_FCALL                                               'array_push'
         13        SEND_REF                                                 !7
         14        FETCH_DIM_R                                      ~12     !1, !8
         15        SEND_VAL                                                 ~12
         16        DO_ICALL                                                 
   15    17        PRE_INC                                                  !8
         18    >   IS_SMALLER                                               !8, !5
         19      > JMPNZ                                                    ~15, ->12
   20    20    >   INIT_FCALL                                               'array_push'
         21        SEND_REF                                                 !6
         22        SEND_VAR                                                 !7
         23        DO_ICALL                                                 
   21    24      > RETURN                                                   null
   24    25    >   ASSIGN                                                   !8, !2
         26      > JMP                                                      ->42
   25    27    >   FETCH_DIM_R                                      ~19     !0, !8
         28        ASSIGN_DIM                                               !1, !4
         29        OP_DATA                                                  ~19
   26    30        INIT_FCALL_BY_NAME                                       'combinationUtil'
         31        SEND_VAR_EX                                              !0
         32        SEND_VAR_EX                                              !1
         33        ADD                                              ~20     !8, 1
         34        SEND_VAL_EX                                              ~20
         35        SEND_VAR_EX                                              !3
         36        ADD                                              ~21     !4, 1
         37        SEND_VAL_EX                                              ~21
         38        SEND_VAR_EX                                              !5
         39        SEND_VAR_EX                                              !6
         40        DO_FCALL                                      0          
   24    41        PRE_INC                                                  !8
         42    >   IS_SMALLER_OR_EQUAL                              ~24     !8, !3
         43      > JMPZ_EX                                          ~24     ~24, ->49
         44    >   SUB                                              ~25     !3, !8
         45        ADD                                              ~26     ~25, 1
         46        SUB                                              ~27     !5, !4
         47        IS_SMALLER_OR_EQUAL                              ~28     ~27, ~26
         48        BOOL                                             ~24     ~28
         49    > > JMPNZ                                                    ~24, ->27
   28    50    > > RETURN                                                   null

End of function combinationutil

Function printvalue:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/KimaE
function name:  printValue
number of ops:  18
compiled vars:  !0 = $arr, !1 = $string, !2 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   31     1        ASSIGN                                                   !1, ''
   32     2      > FE_RESET_R                                       $4      !0, ->8
          3    > > FE_FETCH_R                                               $4, !2, ->8
   33     4    >   CONCAT                                           ~5      '%24', !2
          5        CONCAT                                           ~6      ~5, '%2C+'
          6        ASSIGN_OP                                     8          !1, ~6
   32     7      > JMP                                                      ->3
          8    >   FE_FREE                                                  $4
   35     9        INIT_FCALL                                               'substr'
         10        SEND_VAR                                                 !1
         11        SEND_VAL                                                 0
         12        STRLEN                                           ~8      !1
         13        SUB                                              ~9      ~8, 2
         14        SEND_VAL                                                 ~9
         15        DO_ICALL                                         $10     
         16        ECHO                                                     $10
   36    17      > RETURN                                                   null

End of function printvalue

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.34 ms | 1411 KiB | 19 Q