3v4l.org

run code in 300+ PHP versions simultaneously
<?php $categories = array( array('id'=>1,'name'=>'电脑','pid'=>0), array('id'=>2,'name'=>'手机','pid'=>0), array('id'=>3,'name'=>'笔记本','pid'=>1), array('id'=>4,'name'=>'台式机','pid'=>1), array('id'=>5,'name'=>'智能机','pid'=>2), array('id'=>6,'name'=>'功能机','pid'=>2), array('id'=>7,'name'=>'超级本','pid'=>3), array('id'=>8,'name'=>'游戏本','pid'=>3), ); $tree = array(); //第一步,将分类id作为数组key,并创建children单元 foreach($categories as $category){ $tree[$category['pid']] = $category; $tree[$category['pid']]['parent'] = array(); } echo '<pre>';print_r($tree); //第二部,利用引用,将每个分类添加到父类children数组中,这样一次遍历即可形成树形结构。 foreach ($tree as $k=>$item) { if ($item['pid'] != 0) { $tree[$item['pid']]['parent'][] = &$tree[$k]; } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 31
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 31
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 30
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 30
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
Branch analysis from position: 12
filename:       /in/bXITd
function name:  (null)
number of ops:  33
compiled vars:  !0 = $categories, !1 = $tree, !2 = $category, !3 = $item, !4 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, <array>
   14     1        ASSIGN                                                   !1, <array>
   16     2      > FE_RESET_R                                       $7      !0, ->12
          3    > > FE_FETCH_R                                               $7, !2, ->12
   17     4    >   FETCH_DIM_R                                      ~8      !2, 'pid'
          5        ASSIGN_DIM                                               !1, ~8
          6        OP_DATA                                                  !2
   18     7        FETCH_DIM_R                                      ~10     !2, 'pid'
          8        FETCH_DIM_W                                      $11     !1, ~10
          9        ASSIGN_DIM                                               $11, 'parent'
         10        OP_DATA                                                  <array>
   16    11      > JMP                                                      ->3
         12    >   FE_FREE                                                  $7
   20    13        ECHO                                                     '%3Cpre%3E'
         14        INIT_FCALL                                               'print_r'
         15        SEND_VAR                                                 !1
         16        DO_ICALL                                                 
   22    17      > FE_RESET_R                                       $14     !1, ->31
         18    > > FE_FETCH_R                                       ~15     $14, !3, ->31
         19    >   ASSIGN                                                   !4, ~15
   23    20        FETCH_DIM_R                                      ~17     !3, 'pid'
         21        IS_NOT_EQUAL                                             ~17, 0
         22      > JMPZ                                                     ~18, ->30
   24    23    >   FETCH_DIM_R                                      ~19     !3, 'pid'
         24        FETCH_DIM_W                                      $23     !1, !4
         25        MAKE_REF                                         $24     $23
         26        FETCH_DIM_W                                      $20     !1, ~19
         27        FETCH_DIM_W                                      $21     $20, 'parent'
         28        FETCH_DIM_W                                      $22     $21
         29        ASSIGN_REF                                               $22, $24
   22    30    > > JMP                                                      ->18
         31    >   FE_FREE                                                  $14
   26    32      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
179.9 ms | 1396 KiB | 15 Q