3v4l.org

run code in 500+ PHP versions simultaneously
<?php class CategoryTree { private $catTree = array(); public function addCategory($category, $parent) { if($parent == null){ if(count(array_column($this->catTree, 'category'), $category) > 0){ echo 'Duplicate category'; } } 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('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/BkvjC
function name:  (null)
number of ops:  25
compiled vars:  !0 = $c
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   30     0  E >   NEW                                                  $1      'CategoryTree'
          1        DO_FCALL                                          0          
          2        ASSIGN                                                       !0, $1
   31     3        INIT_METHOD_CALL                                             !0, 'addCategory'
          4        SEND_VAL_EX                                                  'A'
          5        SEND_VAL_EX                                                  null
          6        DO_FCALL                                          0          
   32     7        INIT_METHOD_CALL                                             !0, 'addCategory'
          8        SEND_VAL_EX                                                  'A'
          9        SEND_VAL_EX                                                  null
         10        DO_FCALL                                          0          
   33    11        INIT_METHOD_CALL                                             !0, 'addCategory'
         12        SEND_VAL_EX                                                  'B'
         13        SEND_VAL_EX                                                  'A'
         14        DO_FCALL                                          0          
   35    15        INIT_METHOD_CALL                                             !0, 'addCategory'
         16        SEND_VAL_EX                                                  'C'
         17        SEND_VAL_EX                                                  'A'
         18        DO_FCALL                                          0          
   37    19        INIT_METHOD_CALL                                             !0, 'getChildren'
         20        SEND_VAL_EX                                                  'A'
         21        DO_FCALL                                          0  $8      
         22        FRAMELESS_ICALL_2                implode             ~9      '%2C', $8
         23        ECHO                                                         ~9
         24      > RETURN                                                       1

Class CategoryTree:
Function addcategory:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 17
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
Branch analysis from position: 17
filename:       /in/BkvjC
function name:  addCategory
number of ops:  26
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, ->17
    9     4    >   INIT_FCALL                                                   'count'
          5        INIT_FCALL                                                   'array_column'
          6        FETCH_OBJ_R                                          ~4      'catTree'
          7        SEND_VAL                                                     ~4
          8        SEND_VAL                                                     'category'
          9        DO_ICALL                                             $5      
         10        SEND_VAR                                                     $5
         11        SEND_VAR                                                     !0
         12        DO_ICALL                                             $6      
         13        IS_SMALLER                                                   0, $6
         14      > JMPZ                                                         ~7, ->16
   10    15    >   ECHO                                                         'Duplicate+category'
    8    16    > > JMP                                                          ->17
   16    17    >   INIT_ARRAY                                           ~8      !0, 'category'
         18        ADD_ARRAY_ELEMENT                                    ~8      !1, 'parent'
         19        ASSIGN                                                       !2, ~8
   17    20        INIT_FCALL                                                   'array_push'
         21        FETCH_OBJ_W                                          $10     'catTree'
         22        SEND_REF                                                     $10
         23        SEND_VAR                                                     !2
         24        DO_ICALL                                                     
   18    25      > 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/BkvjC
function name:  getChildren
number of ops:  21
compiled vars:  !0 = $parent, !1 = $that, !2 = $keys
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   20     0  E >   RECV                                                 !0      
   22     1        FETCH_THIS                                           ~3      
          2        ASSIGN                                                       !1, ~3
   23     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
   24    13        INIT_FCALL                                                   'array_map'
         14        DECLARE_LAMBDA_FUNCTION                              ~9      [0]
         15        BIND_LEXICAL                                                 ~9, !1
         16        SEND_VAL                                                     ~9
         17        SEND_VAR                                                     !2
         18        DO_ICALL                                             $10     
         19      > RETURN                                                       $10
   25    20*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BkvjC
function name:  {closure:CategoryTree::getChildren():24}
number of ops:  7
compiled vars:  !0 = $k, !1 = $that
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   24     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 Dynamic Function 0

End of function getchildren

End of class CategoryTree.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
176.29 ms | 3072 KiB | 20 Q