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{ if(count(array_column($this->catTree, 'parent', $parent)) == 0){ echo 'Parent not found'; } else{ $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/RoPh9
function name:  (null)
number of ops:  32
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   NEW                                              $1      'CategoryTree'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   42     3        INIT_METHOD_CALL                                         !0, 'addCategory'
          4        SEND_VAL_EX                                              'A'
          5        SEND_VAL_EX                                              null
          6        DO_FCALL                                      0          
   43     7        INIT_METHOD_CALL                                         !0, 'addCategory'
          8        SEND_VAL_EX                                              'A'
          9        SEND_VAL_EX                                              null
         10        DO_FCALL                                      0          
   44    11        INIT_METHOD_CALL                                         !0, 'addCategory'
         12        SEND_VAL_EX                                              'B'
         13        SEND_VAL_EX                                              'A'
         14        DO_FCALL                                      0          
   45    15        INIT_METHOD_CALL                                         !0, 'addCategory'
         16        SEND_VAL_EX                                              'B'
         17        SEND_VAL_EX                                              'D'
         18        DO_FCALL                                      0          
   46    19        INIT_METHOD_CALL                                         !0, 'addCategory'
         20        SEND_VAL_EX                                              'C'
         21        SEND_VAL_EX                                              'A'
         22        DO_FCALL                                      0          
   48    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%2FRoPh9%3A35%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RoPh9
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $k, !1 = $that
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     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%2FRoPh9%3A35%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 = 43
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 35
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RoPh9
function name:  addCategory
number of ops:  44
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                                                      ->43
   19    24    >   INIT_FCALL                                               'array_column'
         25        FETCH_OBJ_R                                      ~12     'catTree'
         26        SEND_VAL                                                 ~12
         27        SEND_VAL                                                 'parent'
         28        SEND_VAR                                                 !1
         29        DO_ICALL                                         $13     
         30        COUNT                                            ~14     $13
         31        IS_EQUAL                                                 ~14, 0
         32      > JMPZ                                                     ~15, ->35
   20    33    >   ECHO                                                     'Parent+not+found'
         34      > JMP                                                      ->43
   23    35    >   INIT_ARRAY                                       ~16     !0, 'category'
         36        ADD_ARRAY_ELEMENT                                ~16     !1, 'parent'
         37        ASSIGN                                                   !2, ~16
   24    38        INIT_FCALL                                               'array_push'
         39        FETCH_OBJ_W                                      $18     'catTree'
         40        SEND_REF                                                 $18
         41        SEND_VAR                                                 !2
         42        DO_ICALL                                                 
   29    43    > > 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/RoPh9
function name:  getChildren
number of ops:  21
compiled vars:  !0 = $parent, !1 = $that, !2 = $keys
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   33     1        FETCH_THIS                                       ~3      
          2        ASSIGN                                                   !1, ~3
   34     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
   35    13        INIT_FCALL                                               'array_map'
         14        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FRoPh9%3A35%240'
         15        BIND_LEXICAL                                             ~9, !1
         16        SEND_VAL                                                 ~9
         17        SEND_VAR                                                 !2
         18        DO_ICALL                                         $10     
         19      > RETURN                                                   $10
   36    20*     > RETURN                                                   null

End of function getchildren

End of class CategoryTree.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.68 ms | 1404 KiB | 23 Q