3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ [ '5' => [ 'BG' => 50, 'CH' => 60, 'LG' => 50, 'MT' => 40, 'MO' => 80, 'PH' => 60, 'GE' => null, ], ], [ '6' => [ 'BG' => 90, 'CH' => 60, 'LG' => 60, 'MT' => 50, 'MO' => null, 'PH' => 50, 'GE' => null, ], ], [ '7' => [ 'BG' => 80, 'CH' => 55, 'LG' => 65, 'MT' => 50, 'MO' => null, 'PH' => 50, 'GE' => 55, ], ], ]; $keys = array_flip(['BG', 'MT', 'GE']); // best of two keys $Ckeys = array_flip(['CH', 'PH', 'MO', 'LG']); // compulsory keys foreach ($arr as $key => $value) { // use to pass parameters to reduce function during call $temp[key($value)] = array_reduce($value, function (&$result, $a) use ($keys, $Ckeys) { // if mo empty then best of two else best of one $bestHowMany = (!empty($a['MO']) ? 1 : 2); // fetching best of keys from $keys $t = array_intersect_key($a, $keys); // reverse sort and maintains keys arsort($t); // fetch top $bestHowMany Values $largest2 = array_slice($t, 0, $bestHowMany); // fetch compulsory values from CKeys $t1 = array_intersect_key($a, $Ckeys); // sum by merging compulsory and best of $bestHowMany values return array_sum(array_merge($t1, $largest2)); }); } print_r($temp);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 25
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 25
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/J8Opk
function name:  (null)
number of ops:  30
compiled vars:  !0 = $arr, !1 = $keys, !2 = $Ckeys, !3 = $value, !4 = $key, !5 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   36     1        INIT_FCALL                                               'array_flip'
          2        SEND_VAL                                                 <array>
          3        DO_ICALL                                         $7      
          4        ASSIGN                                                   !1, $7
   37     5        INIT_FCALL                                               'array_flip'
          6        SEND_VAL                                                 <array>
          7        DO_ICALL                                         $9      
          8        ASSIGN                                                   !2, $9
   38     9      > FE_RESET_R                                       $11     !0, ->25
         10    > > FE_FETCH_R                                       ~12     $11, !3, ->25
         11    >   ASSIGN                                                   !4, ~12
   40    12        INIT_FCALL                                               'key'
         13        SEND_VAR                                                 !3
         14        DO_ICALL                                         $14     
         15        INIT_FCALL                                               'array_reduce'
         16        SEND_VAR                                                 !3
         17        DECLARE_LAMBDA_FUNCTION                          ~16     [0]
         18        BIND_LEXICAL                                             ~16, !1
         19        BIND_LEXICAL                                             ~16, !2
   53    20        SEND_VAL                                                 ~16
   40    21        DO_ICALL                                         $17     
         22        ASSIGN_DIM                                               !5, $14
   53    23        OP_DATA                                                  $17
   38    24      > JMP                                                      ->10
         25    >   FE_FREE                                                  $11
   55    26        INIT_FCALL                                               'print_r'
         27        SEND_VAR                                                 !5
         28        DO_ICALL                                                 
         29      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/J8Opk
function name:  {closure}
number of ops:  39
compiled vars:  !0 = $result, !1 = $a, !2 = $keys, !3 = $Ckeys, !4 = $bestHowMany, !5 = $t, !6 = $largest2, !7 = $t1
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
   42     4        ISSET_ISEMPTY_DIM_OBJ                         1  ~8      !1, 'MO'
          5        BOOL_NOT                                         ~9      ~8
          6      > JMPZ                                                     ~9, ->9
          7    >   QM_ASSIGN                                        ~10     1
          8      > JMP                                                      ->10
          9    >   QM_ASSIGN                                        ~10     2
         10    >   ASSIGN                                                   !4, ~10
   44    11        INIT_FCALL                                               'array_intersect_key'
         12        SEND_VAR                                                 !1
         13        SEND_VAR                                                 !2
         14        DO_ICALL                                         $12     
         15        ASSIGN                                                   !5, $12
   46    16        INIT_FCALL                                               'arsort'
         17        SEND_REF                                                 !5
         18        DO_ICALL                                                 
   48    19        INIT_FCALL                                               'array_slice'
         20        SEND_VAR                                                 !5
         21        SEND_VAL                                                 0
         22        SEND_VAR                                                 !4
         23        DO_ICALL                                         $15     
         24        ASSIGN                                                   !6, $15
   50    25        INIT_FCALL                                               'array_intersect_key'
         26        SEND_VAR                                                 !1
         27        SEND_VAR                                                 !3
         28        DO_ICALL                                         $17     
         29        ASSIGN                                                   !7, $17
   52    30        INIT_FCALL                                               'array_sum'
         31        INIT_FCALL                                               'array_merge'
         32        SEND_VAR                                                 !7
         33        SEND_VAR                                                 !6
         34        DO_ICALL                                         $19     
         35        SEND_VAR                                                 $19
         36        DO_ICALL                                         $20     
         37      > RETURN                                                   $20
   53    38*     > RETURN                                                   null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.65 ms | 1014 KiB | 22 Q