3v4l.org

run code in 300+ PHP versions simultaneously
<?php function key_values($key) { if ($key[0] == '>') { [$lower, $upper] = [substr($key, 1), 'INF']; } else { [$lower, $upper] = explode('-', $key); } return [$lower, $upper]; } function convert_slabs($slabs) { $bounds = array(); $last_value = reset($slabs); [$lb, $ub] = [$lower, $upper] = key_values(key($slabs)); while ($value = next($slabs)) { [$lower, $upper] = key_values(key($slabs)); // new entry required? if ($value != $last_value) { $bounds[] = (object)['lb' => $lb, 'ub' => $ub, 'value' => $last_value]; [$lb, $ub] = [$lower, $upper]; $last_value = $value; } else { // no, just extend the upper bound $ub = $upper; } } // add the last object $bounds[] = (object)['lb' => $lb, 'ub' => $ub, 'value' => $last_value]; return $bounds; } $slabs_array = array( [ '0-300' => 2.5, '300-500' => 2.5, '500-1000' => 2.5, '1000-5000' => 2.4, '>5000' => 2.4 ], [ '0-300' => 1.2, '300-500' => 1.2, '500-1000' => 1.2, '1000-5000' => 1.3, '>5000' => 1.4 ], [ '0-300' => 2.5, '300-500' => 2.5, '500-1000' => 2.5, '1000-5000' => 2.5, '>5000' => 2.5 ], [ '0-300' => 4.5, '300-500' => 2.5, '500-1000' => 2.5, '1000-5000' => 2.5, '>5000' => 3.5 ], [ '0-300' => 4.5, '300-500' => 3.5, '500-1000' => 3.5, '1000-5000' => 3.5, '>5000' => 3.5 ], [ '0-300' => 1.5, '300-500' => 2.5, '500-1000' => 2.5, '1000-5000' => 2.5, '>5000' => 1.5 ], [ '0-300' => 1.5, '300-500' => 2.5, '500-1000' => 3.5, '1000-5000' => 2.5, '>5000' => 3.5 ], [ '0-300' => 1.5, '300-500' => 2.5, '500-1000' => 2.5, '1000-5000' => 1.5, '>5000' => 1.5 ] ); foreach ($slabs_array as $slabs) { $result = convert_slabs($slabs); echo str_replace('},{', "},\n{", json_encode($result)) . PHP_EOL . PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 19
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 19
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/L2ZEM
function name:  (null)
number of ops:  21
compiled vars:  !0 = $slabs_array, !1 = $slabs, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   ASSIGN                                                   !0, <array>
   46     1      > FE_RESET_R                                       $4      !0, ->19
          2    > > FE_FETCH_R                                               $4, !1, ->19
   47     3    >   INIT_FCALL                                               'convert_slabs'
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $5      
          6        ASSIGN                                                   !2, $5
   48     7        INIT_FCALL                                               'str_replace'
          8        SEND_VAL                                                 '%7D%2C%7B'
          9        SEND_VAL                                                 '%7D%2C%0A%7B'
         10        INIT_FCALL                                               'json_encode'
         11        SEND_VAR                                                 !2
         12        DO_ICALL                                         $7      
         13        SEND_VAR                                                 $7
         14        DO_ICALL                                         $8      
         15        CONCAT                                           ~9      $8, '%0A'
         16        CONCAT                                           ~10     ~9, '%0A'
         17        ECHO                                                     ~10
   46    18      > JMP                                                      ->2
         19    >   FE_FREE                                                  $4
   49    20      > RETURN                                                   1

Function key_values:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 16
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/L2ZEM
function name:  key_values
number of ops:  29
compiled vars:  !0 = $key, !1 = $lower, !2 = $upper
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        FETCH_DIM_R                                      ~3      !0, 0
          2        IS_EQUAL                                                 ~3, '%3E'
          3      > JMPZ                                                     ~4, ->16
    5     4    >   INIT_FCALL                                               'substr'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 1
          7        DO_ICALL                                         $5      
          8        INIT_ARRAY                                       ~6      $5
          9        ADD_ARRAY_ELEMENT                                ~6      'INF'
         10        FETCH_LIST_R                                     $7      ~6, 0
         11        ASSIGN                                                   !1, $7
         12        FETCH_LIST_R                                     $9      ~6, 1
         13        ASSIGN                                                   !2, $9
         14        FREE                                                     ~6
    4    15      > JMP                                                      ->25
    8    16    >   INIT_FCALL                                               'explode'
         17        SEND_VAL                                                 '-'
         18        SEND_VAR                                                 !0
         19        DO_ICALL                                         $11     
         20        FETCH_LIST_R                                     $12     $11, 0
         21        ASSIGN                                                   !1, $12
         22        FETCH_LIST_R                                     $14     $11, 1
         23        ASSIGN                                                   !2, $14
         24        FREE                                                     $11
   10    25    >   INIT_ARRAY                                       ~16     !1
         26        ADD_ARRAY_ELEMENT                                ~16     !2
         27      > RETURN                                                   ~16
   11    28*     > RETURN                                                   null

End of function key_values

Function convert_slabs:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 56, Position 2 = 22
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 50
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
Branch analysis from position: 50
2 jumps found. (Code = 44) Position 1 = 56, Position 2 = 22
Branch analysis from position: 56
Branch analysis from position: 22
filename:       /in/L2ZEM
function name:  convert_slabs
number of ops:  64
compiled vars:  !0 = $slabs, !1 = $bounds, !2 = $last_value, !3 = $lower, !4 = $upper, !5 = $lb, !6 = $ub, !7 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        ASSIGN                                                   !1, <array>
   15     2        INIT_FCALL                                               'reset'
          3        SEND_REF                                                 !0
          4        DO_ICALL                                         $9      
          5        ASSIGN                                                   !2, $9
   16     6        INIT_FCALL                                               'key_values'
          7        INIT_FCALL                                               'key'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $11     
         10        SEND_VAR                                                 $11
         11        DO_FCALL                                      0  $12     
         12        FETCH_LIST_R                                     $13     $12, 0
         13        ASSIGN                                                   !3, $13
         14        FETCH_LIST_R                                     $15     $12, 1
         15        ASSIGN                                                   !4, $15
         16        FETCH_LIST_R                                     $17     $12, 0
         17        ASSIGN                                                   !5, $17
         18        FETCH_LIST_R                                     $19     $12, 1
         19        ASSIGN                                                   !6, $19
         20        FREE                                                     $12
   17    21      > JMP                                                      ->51
   18    22    >   INIT_FCALL                                               'key_values'
         23        INIT_FCALL                                               'key'
         24        SEND_VAR                                                 !0
         25        DO_ICALL                                         $21     
         26        SEND_VAR                                                 $21
         27        DO_FCALL                                      0  $22     
         28        FETCH_LIST_R                                     $23     $22, 0
         29        ASSIGN                                                   !3, $23
         30        FETCH_LIST_R                                     $25     $22, 1
         31        ASSIGN                                                   !4, $25
         32        FREE                                                     $22
   20    33        IS_NOT_EQUAL                                             !7, !2
         34      > JMPZ                                                     ~27, ->50
   21    35    >   INIT_ARRAY                                       ~29     !5, 'lb'
         36        ADD_ARRAY_ELEMENT                                ~29     !6, 'ub'
         37        ADD_ARRAY_ELEMENT                                ~29     !2, 'value'
         38        CAST                                          8  ~30     ~29
         39        ASSIGN_DIM                                               !1
         40        OP_DATA                                                  ~30
   22    41        INIT_ARRAY                                       ~31     !3
         42        ADD_ARRAY_ELEMENT                                ~31     !4
         43        FETCH_LIST_R                                     $32     ~31, 0
         44        ASSIGN                                                   !5, $32
         45        FETCH_LIST_R                                     $34     ~31, 1
         46        ASSIGN                                                   !6, $34
         47        FREE                                                     ~31
   23    48        ASSIGN                                                   !2, !7
   20    49      > JMP                                                      ->51
   27    50    >   ASSIGN                                                   !6, !4
   17    51    >   INIT_FCALL                                               'next'
         52        SEND_REF                                                 !0
         53        DO_ICALL                                         $38     
         54        ASSIGN                                           ~39     !7, $38
         55      > JMPNZ                                                    ~39, ->22
   31    56    >   INIT_ARRAY                                       ~41     !5, 'lb'
         57        ADD_ARRAY_ELEMENT                                ~41     !6, 'ub'
         58        ADD_ARRAY_ELEMENT                                ~41     !2, 'value'
         59        CAST                                          8  ~42     ~41
         60        ASSIGN_DIM                                               !1
         61        OP_DATA                                                  ~42
   32    62      > RETURN                                                   !1
   33    63*     > RETURN                                                   null

End of function convert_slabs

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.24 ms | 1015 KiB | 23 Q