3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CategoryTree { private $catTree = array(); public function addCategory($category, $parent) { if($parent == null){ //echo count(array_column($this->catTree, 'category')); if(count(array_column($this->catTree, 'category', $category)) > 0){ echo 'Duplicate category'; } else{ $temp = array("category" => $category,"parent" => $parent); array_push($this->catTree,$temp); } } else{ echo 'Parent: ' . $parent; //print_r(array_column($this->catTree, 'parent', $parent)); if(count(array_keys(array_column($this->catTree, 'category'), $parent)) == 0){ echo 'Parent not found'; } else{ if(count(array_keys(array_column($this->catTree, 'category'), $category)) == 0){ $temp = array("category" => $category,"parent" => $parent); array_push($this->catTree,$temp); } } } } public function getChildren($parent) { $that = $this; $keys = array_keys(array_column($this->catTree, 'parent'), $parent); return array_map(function($k) use ($that){return $that->catTree[$k]['category'];}, $keys); } } // For testing purposes (do not submit uncommented): $c = new CategoryTree; $c->addCategory('A', null); $c->addCategory('A', null); $c->addCategory('B', 'A'); $c->addCategory('B', 'D'); $c->addCategory('C', 'A'); echo implode(',', $c->getChildren('A'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pHCUu
function name:  (null)
number of ops:  32
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   NEW                                              $1      'CategoryTree'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   45     3        INIT_METHOD_CALL                                         !0, 'addCategory'
          4        SEND_VAL_EX                                              'A'
          5        SEND_VAL_EX                                              null
          6        DO_FCALL                                      0          
   46     7        INIT_METHOD_CALL                                         !0, 'addCategory'
          8        SEND_VAL_EX                                              'A'
          9        SEND_VAL_EX                                              null
         10        DO_FCALL                                      0          
   47    11        INIT_METHOD_CALL                                         !0, 'addCategory'
         12        SEND_VAL_EX                                              'B'
         13        SEND_VAL_EX                                              'A'
         14        DO_FCALL                                      0          
   48    15        INIT_METHOD_CALL                                         !0, 'addCategory'
         16        SEND_VAL_EX                                              'B'
         17        SEND_VAL_EX                                              'D'
         18        DO_FCALL                                      0          
   49    19        INIT_METHOD_CALL                                         !0, 'addCategory'
         20        SEND_VAL_EX                                              'C'
         21        SEND_VAL_EX                                              'A'
         22        DO_FCALL                                      0          
   51    23        INIT_FCALL                                               'implode'
         24        SEND_VAL                                                 '%2C'
         25        INIT_METHOD_CALL                                         !0, 'getChildren'
         26        SEND_VAL_EX                                              'A'
         27        DO_FCALL                                      0  $9      
         28        SEND_VAR                                                 $9
         29        DO_ICALL                                         $10     
         30        ECHO                                                     $10
         31      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FpHCUu%3A38%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pHCUu
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $k, !1 = $that
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        FETCH_OBJ_R                                      ~2      !1, 'catTree'
          3        FETCH_DIM_R                                      ~3      ~2, !0
          4        FETCH_DIM_R                                      ~4      ~3, 'category'
          5      > RETURN                                                   ~4
          6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FpHCUu%3A38%240

Class CategoryTree:
Function addcategory:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 24
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 40
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 60
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
filename:       /in/pHCUu
function name:  addCategory
number of ops:  61
compiled vars:  !0 = $category, !1 = $parent, !2 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    8     2        IS_EQUAL                                                 !1, null
          3      > JMPZ                                                     ~3, ->24
   10     4    >   INIT_FCALL                                               'array_column'
          5        FETCH_OBJ_R                                      ~4      'catTree'
          6        SEND_VAL                                                 ~4
          7        SEND_VAL                                                 'category'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $5      
         10        COUNT                                            ~6      $5
         11        IS_SMALLER                                               0, ~6
         12      > JMPZ                                                     ~7, ->15
   11    13    >   ECHO                                                     'Duplicate+category'
         14      > JMP                                                      ->23
   14    15    >   INIT_ARRAY                                       ~8      !0, 'category'
         16        ADD_ARRAY_ELEMENT                                ~8      !1, 'parent'
         17        ASSIGN                                                   !2, ~8
   15    18        INIT_FCALL                                               'array_push'
         19        FETCH_OBJ_W                                      $10     'catTree'
         20        SEND_REF                                                 $10
         21        SEND_VAR                                                 !2
         22        DO_ICALL                                                 
         23    > > JMP                                                      ->60
   19    24    >   CONCAT                                           ~12     'Parent%3A+', !1
         25        ECHO                                                     ~12
   21    26        INIT_FCALL                                               'array_keys'
         27        INIT_FCALL                                               'array_column'
         28        FETCH_OBJ_R                                      ~13     'catTree'
         29        SEND_VAL                                                 ~13
         30        SEND_VAL                                                 'category'
         31        DO_ICALL                                         $14     
         32        SEND_VAR                                                 $14
         33        SEND_VAR                                                 !1
         34        DO_ICALL                                         $15     
         35        COUNT                                            ~16     $15
         36        IS_EQUAL                                                 ~16, 0
         37      > JMPZ                                                     ~17, ->40
   22    38    >   ECHO                                                     'Parent+not+found'
         39      > JMP                                                      ->60
   25    40    >   INIT_FCALL                                               'array_keys'
         41        INIT_FCALL                                               'array_column'
         42        FETCH_OBJ_R                                      ~18     'catTree'
         43        SEND_VAL                                                 ~18
         44        SEND_VAL                                                 'category'
         45        DO_ICALL                                         $19     
         46        SEND_VAR                                                 $19
         47        SEND_VAR                                                 !0
         48        DO_ICALL                                         $20     
         49        COUNT                                            ~21     $20
         50        IS_EQUAL                                                 ~21, 0
         51      > JMPZ                                                     ~22, ->60
   26    52    >   INIT_ARRAY                                       ~23     !0, 'category'
         53        ADD_ARRAY_ELEMENT                                ~23     !1, 'parent'
         54        ASSIGN                                                   !2, ~23
   27    55        INIT_FCALL                                               'array_push'
         56        FETCH_OBJ_W                                      $25     'catTree'
         57        SEND_REF                                                 $25
         58        SEND_VAR                                                 !2
         59        DO_ICALL                                                 
   32    60    > > RETURN                                                   null

End of function addcategory

Function getchildren:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pHCUu
function name:  getChildren
number of ops:  21
compiled vars:  !0 = $parent, !1 = $that, !2 = $keys
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   36     1        FETCH_THIS                                       ~3      
          2        ASSIGN                                                   !1, ~3
   37     3        INIT_FCALL                                               'array_keys'
          4        INIT_FCALL                                               'array_column'
          5        FETCH_OBJ_R                                      ~5      'catTree'
          6        SEND_VAL                                                 ~5
          7        SEND_VAL                                                 'parent'
          8        DO_ICALL                                         $6      
          9        SEND_VAR                                                 $6
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $7      
         12        ASSIGN                                                   !2, $7
   38    13        INIT_FCALL                                               'array_map'
         14        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FpHCUu%3A38%240'
         15        BIND_LEXICAL                                             ~9, !1
         16        SEND_VAL                                                 ~9
         17        SEND_VAR                                                 !2
         18        DO_ICALL                                         $10     
         19      > RETURN                                                   $10
   39    20*     > RETURN                                                   null

End of function getchildren

End of class CategoryTree.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
178.56 ms | 1408 KiB | 23 Q