3v4l.org

run code in 300+ PHP versions simultaneously
<?php // https://axenov.dev/php-trees-without-recursion/ // функция построения дерева без рекурсии function tree( array $flat_list, string $fk = 'hid', string $pk = 'id', ): array { $tree = []; foreach ($flat_list as &$node) { $references[$node[$pk]] = &$node; if (empty($node[$fk])) { $tree[$node[$pk]] = &$node; } else { $references[$node[$fk]]['children'][] = &$node; } } return $tree; } // функция для преобразования дерева в плоский список function flatten( array $tree, string $branching = 'children' ): array { $result = []; while (count($tree)) { $node = array_shift($tree); if (!empty($node[$branching])) { $tree = array_merge($tree, $node[$branching]); unset($node[$branching]); } $result[] = $node; } return $result; } //============================================ // Кейс 1. Допустим, массив неассоциирован //============================================ // пример вспомогательной фукции для ассоциации массива function key_by_field(array $flat_list, string $pk = 'id'): array { $result = []; foreach ($flat_list as $element) { $result[$element[$pk]] = $element; } return $result; } // исходные данные $data = <<<JSON [ { "id": 1, "hid": null, "title": "Root" }, { "id": 2, "hid": 1, "title": "Branch 1" }, { "id": 3, "hid": 2, "title": "Leaf 1.1" }, { "id": 5, "hid": 1, "title": "Branch 2" }, { "id": 6, "hid": 5, "title": "Leaf 2.1" }, { "id": 4, "hid": 2, "title": "Leaf 1.2" }, { "id": 7, "hid": 5, "title": "Leaf 2.2" } ] JSON; $json = json_decode($data, JSON_OBJECT_AS_ARRAY | JSON_THROW_ON_ERROR); $json = key_by_field($json); // формирование нового списка элементов print_r($tree = tree($json)); // обратное преобразование в плоский список print_r(flatten($tree)); //============================================ // Кейс 2. Допустим, массив ассоциирован //============================================ $data2 = <<<JSON { "1": { "id": 1, "hid": null, "title": "Root" }, "3": { "id": 2, "hid": 2, "title": "Leaf 1.1" }, "2": { "id": 3, "hid": 1, "title": "Branch 1" }, "5": { "id": 5, "hid": 1, "title": "Branch 2" }, "6": { "id": 6, "hid": 5, "title": "Leaf 2.1" }, "4": { "id": 4, "hid": 2, "title": "Leaf 1.2" }, "7": { "id": 7, "hid": 5, "title": "Leaf 2.2" } } JSON; $json2 = json_decode($data2, JSON_OBJECT_AS_ARRAY | JSON_THROW_ON_ERROR); // формирование нового списка элементов print_r($tree = tree($json)); // обратное преобразование в плоский список print_r(flatten($tree));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WZ20L
function name:  (null)
number of ops:  43
compiled vars:  !0 = $data, !1 = $json, !2 = $tree, !3 = $data2, !4 = $json2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   ASSIGN                                                   !0, '%5B%0A++%7B+%22id%22%3A+1%2C+%22hid%22%3A+null%2C+%22title%22%3A+%22Root%22+%7D%2C%0A++%7B+%22id%22%3A+2%2C+%22hid%22%3A+1%2C+%22title%22%3A+%22Branch+1%22+%7D%2C%0A++%7B+%22id%22%3A+3%2C+%22hid%22%3A+2%2C+%22title%22%3A+%22Leaf+1.1%22+%7D%2C%0A++%7B+%22id%22%3A+5%2C+%22hid%22%3A+1%2C+%22title%22%3A+%22Branch+2%22+%7D%2C%0A++%7B+%22id%22%3A+6%2C+%22hid%22%3A+5%2C+%22title%22%3A+%22Leaf+2.1%22+%7D%2C%0A++%7B+%22id%22%3A+4%2C+%22hid%22%3A+2%2C+%22title%22%3A+%22Leaf+1.2%22+%7D%2C%0A++%7B+%22id%22%3A+7%2C+%22hid%22%3A+5%2C+%22title%22%3A+%22Leaf+2.2%22+%7D%0A%5D'
   66     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 4194305
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !1, $6
   67     6        INIT_FCALL                                               'key_by_field'
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0  $8      
          9        ASSIGN                                                   !1, $8
   70    10        INIT_FCALL                                               'print_r'
         11        INIT_FCALL                                               'tree'
         12        SEND_VAR                                                 !1
         13        DO_FCALL                                      0  $10     
         14        ASSIGN                                           ~11     !2, $10
         15        SEND_VAL                                                 ~11
         16        DO_ICALL                                                 
   73    17        INIT_FCALL                                               'print_r'
         18        INIT_FCALL                                               'flatten'
         19        SEND_VAR                                                 !2
         20        DO_FCALL                                      0  $13     
         21        SEND_VAR                                                 $13
         22        DO_ICALL                                                 
   80    23        ASSIGN                                                   !3, '%7B%0A++%221%22%3A+%7B+%22id%22%3A+1%2C+%22hid%22%3A+null%2C+%22title%22%3A+%22Root%22+%7D%2C%0A++%223%22%3A+%7B+%22id%22%3A+2%2C+%22hid%22%3A+2%2C+%22title%22%3A+%22Leaf+1.1%22+%7D%2C%0A++%222%22%3A+%7B+%22id%22%3A+3%2C+%22hid%22%3A+1%2C+%22title%22%3A+%22Branch+1%22+%7D%2C%0A++%225%22%3A+%7B+%22id%22%3A+5%2C+%22hid%22%3A+1%2C+%22title%22%3A+%22Branch+2%22+%7D%2C%0A++%226%22%3A+%7B+%22id%22%3A+6%2C+%22hid%22%3A+5%2C+%22title%22%3A+%22Leaf+2.1%22+%7D%2C%0A++%224%22%3A+%7B+%22id%22%3A+4%2C+%22hid%22%3A+2%2C+%22title%22%3A+%22Leaf+1.2%22+%7D%2C%0A++%227%22%3A+%7B+%22id%22%3A+7%2C+%22hid%22%3A+5%2C+%22title%22%3A+%22Leaf+2.2%22+%7D%0A%7D'
   91    24        INIT_FCALL                                               'json_decode'
         25        SEND_VAR                                                 !3
         26        SEND_VAL                                                 4194305
         27        DO_ICALL                                         $16     
         28        ASSIGN                                                   !4, $16
   94    29        INIT_FCALL                                               'print_r'
         30        INIT_FCALL                                               'tree'
         31        SEND_VAR                                                 !1
         32        DO_FCALL                                      0  $18     
         33        ASSIGN                                           ~19     !2, $18
         34        SEND_VAL                                                 ~19
         35        DO_ICALL                                                 
   97    36        INIT_FCALL                                               'print_r'
         37        INIT_FCALL                                               'flatten'
         38        SEND_VAR                                                 !2
         39        DO_FCALL                                      0  $21     
         40        SEND_VAR                                                 $21
         41        DO_ICALL                                                 
         42      > RETURN                                                   1

Function tree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 5, Position 2 = 21
Branch analysis from position: 5
2 jumps found. (Code = 126) Position 1 = 6, Position 2 = 21
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/WZ20L
function name:  tree
number of ops:  26
compiled vars:  !0 = $flat_list, !1 = $fk, !2 = $pk, !3 = $tree, !4 = $node, !5 = $references
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'hid'
          2        RECV_INIT                                        !2      'id'
   11     3        ASSIGN                                                   !3, <array>
   12     4      > FE_RESET_RW                                      $7      !0, ->21
          5    > > FE_FETCH_RW                                              $7, !4, ->21
   13     6    >   FETCH_DIM_R                                      ~8      !4, !2
          7        FETCH_DIM_W                                      $9      !5, ~8
          8        ASSIGN_REF                                               $9, !4
   14     9        ISSET_ISEMPTY_DIM_OBJ                         1          !4, !1
         10      > JMPZ                                                     ~11, ->15
   15    11    >   FETCH_DIM_R                                      ~12     !4, !2
         12        FETCH_DIM_W                                      $13     !3, ~12
         13        ASSIGN_REF                                               $13, !4
         14      > JMP                                                      ->20
   17    15    >   FETCH_DIM_R                                      ~15     !4, !1
         16        FETCH_DIM_W                                      $16     !5, ~15
         17        FETCH_DIM_W                                      $17     $16, 'children'
         18        FETCH_DIM_W                                      $18     $17
         19        ASSIGN_REF                                               $18, !4
   12    20    > > JMP                                                      ->5
         21    >   FE_FREE                                                  $7
   20    22        VERIFY_RETURN_TYPE                                       !3
         23      > RETURN                                                   !3
   21    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function tree

Function flatten:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 4
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 18
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 4
Branch analysis from position: 22
Branch analysis from position: 4
Branch analysis from position: 18
filename:       /in/WZ20L
function name:  flatten
number of ops:  26
compiled vars:  !0 = $tree, !1 = $branching, !2 = $result, !3 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'children'
   28     2        ASSIGN                                                   !2, <array>
   29     3      > JMP                                                      ->20
   30     4    >   INIT_FCALL                                               'array_shift'
          5        SEND_REF                                                 !0
          6        DO_ICALL                                         $5      
          7        ASSIGN                                                   !3, $5
   31     8        ISSET_ISEMPTY_DIM_OBJ                         1  ~7      !3, !1
          9        BOOL_NOT                                         ~8      ~7
         10      > JMPZ                                                     ~8, ->18
   32    11    >   INIT_FCALL                                               'array_merge'
         12        SEND_VAR                                                 !0
         13        FETCH_DIM_R                                      ~9      !3, !1
         14        SEND_VAL                                                 ~9
         15        DO_ICALL                                         $10     
         16        ASSIGN                                                   !0, $10
   33    17        UNSET_DIM                                                !3, !1
   35    18    >   ASSIGN_DIM                                               !2
         19        OP_DATA                                                  !3
   29    20    >   COUNT                                            ~13     !0
         21      > JMPNZ                                                    ~13, ->4
   37    22    >   VERIFY_RETURN_TYPE                                       !2
         23      > RETURN                                                   !2
   38    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function flatten

Function key_by_field:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/WZ20L
function name:  key_by_field
number of ops:  14
compiled vars:  !0 = $flat_list, !1 = $pk, !2 = $result, !3 = $element
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'id'
   47     2        ASSIGN                                                   !2, <array>
   48     3      > FE_RESET_R                                       $5      !0, ->9
          4    > > FE_FETCH_R                                               $5, !3, ->9
   49     5    >   FETCH_DIM_R                                      ~6      !3, !1
          6        ASSIGN_DIM                                               !2, ~6
          7        OP_DATA                                                  !3
   48     8      > JMP                                                      ->4
          9    >   FE_FREE                                                  $5
   51    10        VERIFY_RETURN_TYPE                                       !2
         11      > RETURN                                                   !2
   52    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of function key_by_field

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.27 ms | 1399 KiB | 26 Q