3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [11, 3, 45, 6, 61, 89, 22]; function array_slimmer(array $array, int $finalCount): array { // no work to be done if we're asking for more than what the array holds // same goes if we're asking for just one array or (nonsensical) less if ($finalCount >= count($array) || $finalCount < 2) { return $array; } return array_map(function (array $chunk) { // rounded to two decimals, but you can modify to accomodate your needs return round(array_sum($chunk) / count($chunk), 2); }, custom_chunk($array, $finalCount)); } function custom_chunk($array, $maxrows) { $size = sizeof($array); $columns = ceil($size / $maxrows); $fullrows = $size - ($columns - 1) * $maxrows; for ($i = 0; $i < $maxrows; ++$i) { $result[] = array_splice($array, 0, ($i < $fullrows ? $columns : $columns - 1)); } return $result; } print_r(array_slimmer($array, 2)); print_r(array_slimmer($array, 3)); print_r(array_slimmer($array, 4));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZNUsq
function name:  (null)
number of ops:  23
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   29     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'array_slimmer'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 2
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                                 
   30     8        INIT_FCALL                                               'print_r'
          9        INIT_FCALL                                               'array_slimmer'
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 3
         12        DO_FCALL                                      0  $4      
         13        SEND_VAR                                                 $4
         14        DO_ICALL                                                 
   31    15        INIT_FCALL                                               'print_r'
         16        INIT_FCALL                                               'array_slimmer'
         17        SEND_VAR                                                 !0
         18        SEND_VAL                                                 4
         19        DO_FCALL                                      0  $6      
         20        SEND_VAR                                                 $6
         21        DO_ICALL                                                 
         22      > RETURN                                                   1

Function array_slimmer:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/ZNUsq
function name:  array_slimmer
number of ops:  23
compiled vars:  !0 = $array, !1 = $finalCount
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    9     2        COUNT                                            ~2      !0
          3        IS_SMALLER_OR_EQUAL                              ~3      ~2, !1
          4      > JMPNZ_EX                                         ~3      ~3, ->7
          5    >   IS_SMALLER                                       ~4      !1, 2
          6        BOOL                                             ~3      ~4
          7    > > JMPZ                                                     ~3, ->10
   10     8    >   VERIFY_RETURN_TYPE                                       !0
          9      > RETURN                                                   !0
   12    10    >   INIT_FCALL                                               'array_map'
         11        DECLARE_LAMBDA_FUNCTION                          ~5      [0]
   15    12        SEND_VAL                                                 ~5
         13        INIT_FCALL_BY_NAME                                       'custom_chunk'
         14        SEND_VAR_EX                                              !0
         15        SEND_VAR_EX                                              !1
         16        DO_FCALL                                      0  $6      
         17        SEND_VAR                                                 $6
   12    18        DO_ICALL                                         $7      
   15    19        VERIFY_RETURN_TYPE                                       $7
         20      > RETURN                                                   $7
   16    21*       VERIFY_RETURN_TYPE                                       
         22*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZNUsq
function name:  {closure}
number of ops:  12
compiled vars:  !0 = $chunk
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   14     1        INIT_FCALL                                               'round'
          2        INIT_FCALL                                               'array_sum'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5        COUNT                                            ~2      !0
          6        DIV                                              ~3      $1, ~2
          7        SEND_VAL                                                 ~3
          8        SEND_VAL                                                 2
          9        DO_ICALL                                         $4      
         10      > RETURN                                                   $4
   15    11*     > RETURN                                                   null

End of Dynamic Function 0

End of function array_slimmer

Function custom_chunk:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 15
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 15
Branch analysis from position: 31
Branch analysis from position: 15
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 15
Branch analysis from position: 31
Branch analysis from position: 15
filename:       /in/ZNUsq
function name:  custom_chunk
number of ops:  33
compiled vars:  !0 = $array, !1 = $maxrows, !2 = $size, !3 = $columns, !4 = $fullrows, !5 = $i, !6 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   19     2        COUNT                                            ~7      !0
          3        ASSIGN                                                   !2, ~7
   20     4        INIT_FCALL                                               'ceil'
          5        DIV                                              ~9      !2, !1
          6        SEND_VAL                                                 ~9
          7        DO_ICALL                                         $10     
          8        ASSIGN                                                   !3, $10
   21     9        SUB                                              ~12     !3, 1
         10        MUL                                              ~13     !1, ~12
         11        SUB                                              ~14     !2, ~13
         12        ASSIGN                                                   !4, ~14
   23    13        ASSIGN                                                   !5, 0
         14      > JMP                                                      ->29
   24    15    >   INIT_FCALL                                               'array_splice'
         16        SEND_REF                                                 !0
         17        SEND_VAL                                                 0
         18        IS_SMALLER                                               !5, !4
         19      > JMPZ                                                     ~18, ->22
         20    >   QM_ASSIGN                                        ~19     !3
         21      > JMP                                                      ->24
         22    >   SUB                                              ~20     !3, 1
         23        QM_ASSIGN                                        ~19     ~20
         24    >   SEND_VAL                                                 ~19
         25        DO_ICALL                                         $21     
         26        ASSIGN_DIM                                               !6
         27        OP_DATA                                                  $21
   23    28        PRE_INC                                                  !5
         29    >   IS_SMALLER                                               !5, !1
         30      > JMPNZ                                                    ~23, ->15
   26    31    > > RETURN                                                   !6
   27    32*     > RETURN                                                   null

End of function custom_chunk

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.66 ms | 1027 KiB | 22 Q