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); } 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/ttI59
function name:  (null)
number of ops:  28
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   NEW                                              $1      'CategoryTree'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   32     3        INIT_METHOD_CALL                                         !0, 'addCategory'
          4        SEND_VAL_EX                                              'A'
          5        SEND_VAL_EX                                              null
          6        DO_FCALL                                      0          
   33     7        INIT_METHOD_CALL                                         !0, 'addCategory'
          8        SEND_VAL_EX                                              'A'
          9        SEND_VAL_EX                                              null
         10        DO_FCALL                                      0          
   34    11        INIT_METHOD_CALL                                         !0, 'addCategory'
         12        SEND_VAL_EX                                              'B'
         13        SEND_VAL_EX                                              'A'
         14        DO_FCALL                                      0          
   36    15        INIT_METHOD_CALL                                         !0, 'addCategory'
         16        SEND_VAL_EX                                              'C'
         17        SEND_VAL_EX                                              'A'
         18        DO_FCALL                                      0          
   38    19        INIT_FCALL                                               'implode'
         20        SEND_VAL                                                 '%2C'
         21        INIT_METHOD_CALL                                         !0, 'getChildren'
         22        SEND_VAL_EX                                              'A'
         23        DO_FCALL                                      0  $8      
         24        SEND_VAR                                                 $8
         25        DO_ICALL                                         $9      
         26        ECHO                                                     $9
         27      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FttI59%3A25%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ttI59
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $k, !1 = $that
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     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%2FttI59%3A25%240

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

End of function getchildren

End of class CategoryTree.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.48 ms | 1404 KiB | 23 Q