3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array ( 0 => array ( 'id' => 1, 'name' => 'Пользователи', 'alias' => 'users', 'childrens' => array ( 0 => array ( 'id' => 2, 'name' => 'Создание', 'alias' => 'create', ), 1 => array ( 'id' => 3, 'name' => 'Список', 'alias' => 'list', 'childrens' => array ( 0 => array ( 'id' => 4, 'name' => 'Активные', 'alias' => 'active', ), 1 => array ( 'id' => 5, 'name' => 'Удаленные', 'alias' => 'deleted', ), ), ), 2 => array ( 'id' => 8, 'name' => 'Поиск', 'alias' => 'search', ), ), ), 1 => array ( 'id' => 6, 'name' => 'Заявки', 'alias' => 'requests', 'childrens' => array ( 0 => array ( 'id' => 9, 'name' => 'Заявки на поключение', 'alias' => 'connecting', ), 1 => array ( 'id' => 10, 'name' => 'Заявки на ремонт', 'alias' => 'repairs', ), 2 => array ( 'id' => 11, 'name' => 'Заявки на обход', 'alias' => 'round', ), ), ), 2 => array ( 'id' => 7, 'name' => 'Отчёты', 'alias' => 'reports', 'childrens' => array ( 0 => array ( 'id' => 12, 'name' => 'Отдел маркетинга', 'alias' => 'marketing', 'childrens' => array ( 0 => array ( 'id' => 15, 'name' => 'Отчёт по списаниям', 'alias' => 'write-offs', ), 1 => array ( 'id' => 16, 'name' => 'Отчёт по расходам', 'alias' => 'costs', ), 2 => array ( 'id' => 17, 'name' => 'Годовой отчёт', 'alias' => 'year', ), ), ), 1 => array ( 'id' => 14, 'name' => 'Управление', 'alias' => 'control', 0 => array ( 0 => array ( 'id' => 18, 'name' => 'Отчёт по эффективности работы', 'alias' => 'efficiency', ), 1 => array ( 'id' => 19, 'name' => 'Отчёт по подключениям', 'alias' => 'connecting', ), ), ), ), ), ); function flatten($arr) { $ret = []; foreach ($arr as $item) { $tmp = ['id' => $item['id'], 'name' => $item['name'], 'alias' => $item['alias'], ]; $hasChildren = array_key_exists('childrens', $item); if ($hasChildren) { $tmp['childrens'] = array_reduce( $item['childrens'], function ($prev, $_item) {return ltrim($prev . ', ' . $_item['name'], ', ');}, '' ); } $ret[] = $tmp; if ($hasChildren) { $ret = array_merge($ret, flatten($item['childrens'])); } } return $ret; } var_dump(flatten($arr));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IHJHe
function name:  (null)
number of ops:  8
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
  157     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'flatten'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function flatten:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 37
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 37
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 23
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 36
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 36
Branch analysis from position: 23
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename:       /in/IHJHe
function name:  flatten
number of ops:  40
compiled vars:  !0 = $arr, !1 = $ret, !2 = $item, !3 = $tmp, !4 = $hasChildren
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  132     0  E >   RECV                                             !0      
  134     1        ASSIGN                                                   !1, <array>
  135     2      > FE_RESET_R                                       $6      !0, ->37
          3    > > FE_FETCH_R                                               $6, !2, ->37
  136     4    >   FETCH_DIM_R                                      ~7      !2, 'id'
          5        INIT_ARRAY                                       ~8      ~7, 'id'
          6        FETCH_DIM_R                                      ~9      !2, 'name'
          7        ADD_ARRAY_ELEMENT                                ~8      ~9, 'name'
          8        FETCH_DIM_R                                      ~10     !2, 'alias'
          9        ADD_ARRAY_ELEMENT                                ~8      ~10, 'alias'
         10        ASSIGN                                                   !3, ~8
  137    11        ARRAY_KEY_EXISTS                                 ~12     'childrens', !2
         12        ASSIGN                                                   !4, ~12
  138    13      > JMPZ                                                     !4, ->23
  139    14    >   INIT_FCALL                                               'array_reduce'
  140    15        FETCH_DIM_R                                      ~15     !2, 'childrens'
         16        SEND_VAL                                                 ~15
  141    17        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FIHJHe%3A141%240'
         18        SEND_VAL                                                 ~16
  142    19        SEND_VAL                                                 ''
         20        DO_ICALL                                         $17     
  139    21        ASSIGN_DIM                                               !3, 'childrens'
  142    22        OP_DATA                                                  $17
  146    23    >   ASSIGN_DIM                                               !1
         24        OP_DATA                                                  !3
  148    25      > JMPZ                                                     !4, ->36
  149    26    >   INIT_FCALL                                               'array_merge'
         27        SEND_VAR                                                 !1
         28        INIT_FCALL_BY_NAME                                       'flatten'
         29        CHECK_FUNC_ARG                                           
         30        FETCH_DIM_FUNC_ARG                               $19     !2, 'childrens'
         31        SEND_FUNC_ARG                                            $19
         32        DO_FCALL                                      0  $20     
         33        SEND_VAR                                                 $20
         34        DO_ICALL                                         $21     
         35        ASSIGN                                                   !1, $21
  135    36    > > JMP                                                      ->3
         37    >   FE_FREE                                                  $6
  154    38      > RETURN                                                   !1
  155    39*     > RETURN                                                   null

End of function flatten

Function %00%7Bclosure%7D%2Fin%2FIHJHe%3A141%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/IHJHe
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $prev, !1 = $_item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  141     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        INIT_FCALL                                               'ltrim'
          3        CONCAT                                           ~2      !0, '%2C+'
          4        FETCH_DIM_R                                      ~3      !1, 'name'
          5        CONCAT                                           ~4      ~2, ~3
          6        SEND_VAL                                                 ~4
          7        SEND_VAL                                                 '%2C+'
          8        DO_ICALL                                         $5      
          9      > RETURN                                                   $5
         10*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FIHJHe%3A141%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.75 ms | 1407 KiB | 22 Q