3v4l.org

run code in 300+ PHP versions simultaneously
<?php // https://axenov.dev/php-trees-without-recursion/ // функция восстановления иерархии function hierarchy( array $flat_list, array $node, string $fk = 'hid', string $pk = 'id', ): array { $stack[$node[$pk]] = $node; while (!empty($flat_list[$node[$fk]])) { $node = $flat_list[$node[$fk]]; $stack[$node[$pk]] = $node; } return $stack; } //============================================ // Кейс 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(hierarchy($json, $json[7])); //============================================ // Кейс 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(hierarchy($json2, $json[7]));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JO7OC
function name:  (null)
number of ops:  33
compiled vars:  !0 = $data, !1 = $json, !2 = $data2, !3 = $json2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     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'
   46     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 4194305
          4        DO_ICALL                                         $5      
          5        ASSIGN                                                   !1, $5
   47     6        INIT_FCALL                                               'key_by_field'
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0  $7      
          9        ASSIGN                                                   !1, $7
   50    10        INIT_FCALL                                               'print_r'
         11        INIT_FCALL                                               'hierarchy'
         12        SEND_VAR                                                 !1
         13        FETCH_DIM_R                                      ~9      !1, 7
         14        SEND_VAL                                                 ~9
         15        DO_FCALL                                      0  $10     
         16        SEND_VAR                                                 $10
         17        DO_ICALL                                                 
   58    18        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'
   69    19        INIT_FCALL                                               'json_decode'
         20        SEND_VAR                                                 !2
         21        SEND_VAL                                                 4194305
         22        DO_ICALL                                         $13     
         23        ASSIGN                                                   !3, $13
   72    24        INIT_FCALL                                               'print_r'
         25        INIT_FCALL                                               'hierarchy'
         26        SEND_VAR                                                 !3
         27        FETCH_DIM_R                                      ~15     !1, 7
         28        SEND_VAL                                                 ~15
         29        DO_FCALL                                      0  $16     
         30        SEND_VAR                                                 $16
         31        DO_ICALL                                                 
         32      > RETURN                                                   1

Function hierarchy:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 8
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 8
Branch analysis from position: 18
Branch analysis from position: 8
filename:       /in/JO7OC
function name:  hierarchy
number of ops:  22
compiled vars:  !0 = $flat_list, !1 = $node, !2 = $fk, !3 = $pk, !4 = $stack
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      'hid'
          3        RECV_INIT                                        !3      'id'
   12     4        FETCH_DIM_R                                      ~5      !1, !3
          5        ASSIGN_DIM                                               !4, ~5
          6        OP_DATA                                                  !1
   13     7      > JMP                                                      ->14
   14     8    >   FETCH_DIM_R                                      ~7      !1, !2
          9        FETCH_DIM_R                                      ~8      !0, ~7
         10        ASSIGN                                                   !1, ~8
   15    11        FETCH_DIM_R                                      ~10     !1, !3
         12        ASSIGN_DIM                                               !4, ~10
         13        OP_DATA                                                  !1
   13    14    >   FETCH_DIM_R                                      ~12     !1, !2
         15        ISSET_ISEMPTY_DIM_OBJ                         1  ~13     !0, ~12
         16        BOOL_NOT                                         ~14     ~13
         17      > JMPNZ                                                    ~14, ->8
   17    18    >   VERIFY_RETURN_TYPE                                       !4
         19      > RETURN                                                   !4
   18    20*       VERIFY_RETURN_TYPE                                       
         21*     > RETURN                                                   null

End of function hierarchy

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/JO7OC
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
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'id'
   27     2        ASSIGN                                                   !2, <array>
   28     3      > FE_RESET_R                                       $5      !0, ->9
          4    > > FE_FETCH_R                                               $5, !3, ->9
   29     5    >   FETCH_DIM_R                                      ~6      !3, !1
          6        ASSIGN_DIM                                               !2, ~6
          7        OP_DATA                                                  !3
   28     8      > JMP                                                      ->4
          9    >   FE_FREE                                                  $5
   31    10        VERIFY_RETURN_TYPE                                       !2
         11      > RETURN                                                   !2
   32    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of function key_by_field

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
130.55 ms | 1411 KiB | 20 Q