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; } //============================================ // Кейс 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($json)); //============================================ // Кейс 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($json2));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1uBdT
function name:  (null)
number of ops:  29
compiled vars:  !0 = $data, !1 = $json, !2 = $data2, !3 = $json2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     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'
   49     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 4194305
          4        DO_ICALL                                         $5      
          5        ASSIGN                                                   !1, $5
   50     6        INIT_FCALL                                               'key_by_field'
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0  $7      
          9        ASSIGN                                                   !1, $7
   53    10        INIT_FCALL                                               'print_r'
         11        INIT_FCALL                                               'tree'
         12        SEND_VAR                                                 !1
         13        DO_FCALL                                      0  $9      
         14        SEND_VAR                                                 $9
         15        DO_ICALL                                                 
   61    16        ASSIGN                                                   !2, '%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'
   72    17        INIT_FCALL                                               'json_decode'
         18        SEND_VAR                                                 !2
         19        SEND_VAL                                                 4194305
         20        DO_ICALL                                         $12     
         21        ASSIGN                                                   !3, $12
   75    22        INIT_FCALL                                               'print_r'
         23        INIT_FCALL                                               'tree'
         24        SEND_VAR                                                 !3
         25        DO_FCALL                                      0  $14     
         26        SEND_VAR                                                 $14
         27        DO_ICALL                                                 
         28      > 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/1uBdT
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    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 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/1uBdT
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
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'id'
   30     2        ASSIGN                                                   !2, <array>
   31     3      > FE_RESET_R                                       $5      !0, ->9
          4    > > FE_FETCH_R                                               $5, !3, ->9
   32     5    >   FETCH_DIM_R                                      ~6      !3, !1
          6        ASSIGN_DIM                                               !2, ~6
          7        OP_DATA                                                  !3
   31     8      > JMP                                                      ->4
          9    >   FE_FREE                                                  $5
   34    10        VERIFY_RETURN_TYPE                                       !2
         11      > RETURN                                                   !2
   35    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of function key_by_field

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.75 ms | 1457 KiB | 18 Q