3v4l.org

run code in 300+ PHP versions simultaneously
<?php $mapping = [ new Mapper(['jobname' => 'beruf']), new Mapper(['num_salary' => 'einkommen.anzahlGehaelter']), new Mapper(['netincome' => 'einkommen.netto']), new Mapper(['grossincome' => 'einkommen.brutto']) ]; $payload = [ 'beruf' => 'Maler', 'einkommen' => [ 'anzahlGehaelter' => 12, 'netto' => 51253, 'brutto' => 786522 ] ]; final class Mapper { private $key; private $name; public function __construct(array $mapping) { $this->key = key($mapping); $this->name = current($mapping); } public function extract(array $payload): array { foreach (explode('.', $this->name) as $name) { if (!array_key_exists($name, $payload)) { return []; } $payload = $payload[$name]; } return [ $this->key => $payload ]; } } $output = []; foreach ($mapping as $mapper) { $output = array_merge($output, $mapper->extract($payload)); } print_r($output);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 20, Position 2 = 30
Branch analysis from position: 20
2 jumps found. (Code = 78) Position 1 = 21, Position 2 = 30
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/ab2ls
function name:  (null)
number of ops:  35
compiled vars:  !0 = $mapping, !1 = $payload, !2 = $output, !3 = $mapper
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   NEW                                              $4      'Mapper'
          1        SEND_VAL_EX                                              <array>
          2        DO_FCALL                                      0          
          3        INIT_ARRAY                                       ~6      $4
    5     4        NEW                                              $7      'Mapper'
          5        SEND_VAL_EX                                              <array>
          6        DO_FCALL                                      0          
          7        ADD_ARRAY_ELEMENT                                ~6      $7
    6     8        NEW                                              $9      'Mapper'
          9        SEND_VAL_EX                                              <array>
         10        DO_FCALL                                      0          
         11        ADD_ARRAY_ELEMENT                                ~6      $9
    7    12        NEW                                              $11     'Mapper'
         13        SEND_VAL_EX                                              <array>
         14        DO_FCALL                                      0          
         15        ADD_ARRAY_ELEMENT                                ~6      $11
    3    16        ASSIGN                                                   !0, ~6
   10    17        ASSIGN                                                   !1, <array>
   46    18        ASSIGN                                                   !2, <array>
   47    19      > FE_RESET_R                                       $16     !0, ->30
         20    > > FE_FETCH_R                                               $16, !3, ->30
   48    21    >   INIT_FCALL                                               'array_merge'
         22        SEND_VAR                                                 !2
         23        INIT_METHOD_CALL                                         !3, 'extract'
         24        SEND_VAR_EX                                              !1
         25        DO_FCALL                                      0  $17     
         26        SEND_VAR                                                 $17
         27        DO_ICALL                                         $18     
         28        ASSIGN                                                   !2, $18
   47    29      > JMP                                                      ->20
         30    >   FE_FREE                                                  $16
   51    31        INIT_FCALL                                               'print_r'
         32        SEND_VAR                                                 !2
         33        DO_ICALL                                                 
         34      > RETURN                                                   1

Class Mapper:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ab2ls
function name:  __construct
number of ops:  12
compiled vars:  !0 = $mapping
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   26     1        INIT_FCALL                                               'key'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $2      
          4        ASSIGN_OBJ                                               'key'
          5        OP_DATA                                                  $2
   27     6        INIT_FCALL                                               'current'
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                         $4      
          9        ASSIGN_OBJ                                               'name'
         10        OP_DATA                                                  $4
   28    11      > RETURN                                                   null

End of function __construct

Function extract:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 16
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/ab2ls
function name:  extract
number of ops:  23
compiled vars:  !0 = $payload, !1 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   32     1        INIT_FCALL                                               'explode'
          2        SEND_VAL                                                 '.'
          3        FETCH_OBJ_R                                      ~2      'name'
          4        SEND_VAL                                                 ~2
          5        DO_ICALL                                         $3      
          6      > FE_RESET_R                                       $4      $3, ->16
          7    > > FE_FETCH_R                                               $4, !1, ->16
   33     8    >   ARRAY_KEY_EXISTS                                 ~5      !1, !0
          9        BOOL_NOT                                         ~6      ~5
         10      > JMPZ                                                     ~6, ->13
   34    11    >   FE_FREE                                                  $4
         12      > RETURN                                                   <array>
   37    13    >   FETCH_DIM_R                                      ~7      !0, !1
         14        ASSIGN                                                   !0, ~7
   32    15      > JMP                                                      ->7
         16    >   FE_FREE                                                  $4
   41    17        FETCH_OBJ_R                                      ~9      'key'
         18        INIT_ARRAY                                       ~10     !0, ~9
         19        VERIFY_RETURN_TYPE                                       ~10
         20      > RETURN                                                   ~10
   43    21*       VERIFY_RETURN_TYPE                                       
         22*     > RETURN                                                   null

End of function extract

End of class Mapper.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
180.08 ms | 1400 KiB | 23 Q