3v4l.org

run code in 300+ PHP versions simultaneously
<?php // The hierarcical structure of the categories is: // // Domain // - Category // - - Subcategory class simulating_categories_object{ public $id; public $parent; public $slug; function __construct($id,$parent,$slug){ $this->id = $id; $this->parent = $parent; $this->slug = $slug; } } global $catobj; $catobj = []; $catobj[] = new simulating_categories_object(1,0,'domain1'); $catobj[] = new simulating_categories_object(2,0,'domain2'); $catobj[] = new simulating_categories_object(3,0,'domain3'); $catobj[] = new simulating_categories_object(4,1,'category1'); $catobj[] = new simulating_categories_object(5,1,'category2'); $catobj[] = new simulating_categories_object(6,1,'category3'); $catobj[] = new simulating_categories_object(7,4,'subcategory1'); $catobj[] = new simulating_categories_object(8,4,'subcategory2'); $catobj[] = new simulating_categories_object(9,2,'category555'); $catobj[] = new simulating_categories_object(10,9,'subcategory666'); $catobj[] = new simulating_categories_object(11,3,'category777'); function get_the_parent_id_of_a_given_element_id($id){ global $catobj; foreach($catobj as $category){ if($category->id == $id) return $category->parent; } } echo "<pre>"; //print_r($catobj); echo "</pre>"; foreach($catobj as $element) { if($element->parent == 0){ // our $element is a domain. echo "This element is a domain. The slug of the element is ".$element->slug."<br>\n"; continue; } $element_parent_id = get_the_parent_id_of_a_given_element_id($element->parent); if($element_parent_id == 0){ // our $element is a category echo "This element is a category. The slug of the element is ".$element->slug."<br>\n"; continue; } $element_parent_parent_id = get_the_parent_id_of_a_given_element_id($element_parent_id); if($element_parent_parent_id == 0){ // our $element is a subcategory echo "This element is a subcategory. The slug of the element is ".$element->slug."<br>\n"; } } $result = ['domain1' => [ 'category1' => ['subcategory1','subcategory2'], 'category2' => [], 'category3' => [] ], 'domain2' => [ 'category555' => ['subcategory666'] ], 'domain3' => [ 'category777' => [] ] ]; echo "<pre>"; print_r($result); echo "</pre>";
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 82, Position 2 = 114
Branch analysis from position: 82
2 jumps found. (Code = 78) Position 1 = 83, Position 2 = 114
Branch analysis from position: 83
2 jumps found. (Code = 43) Position 1 = 86, Position 2 = 91
Branch analysis from position: 86
1 jumps found. (Code = 42) Position 1 = 82
Branch analysis from position: 82
Branch analysis from position: 91
2 jumps found. (Code = 43) Position 1 = 98, Position 2 = 103
Branch analysis from position: 98
1 jumps found. (Code = 42) Position 1 = 82
Branch analysis from position: 82
Branch analysis from position: 103
2 jumps found. (Code = 43) Position 1 = 109, Position 2 = 113
Branch analysis from position: 109
1 jumps found. (Code = 42) Position 1 = 82
Branch analysis from position: 82
Branch analysis from position: 113
Branch analysis from position: 114
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 114
filename:       /in/Gm6X1
function name:  (null)
number of ops:  122
compiled vars:  !0 = $catobj, !1 = $element, !2 = $element_parent_id, !3 = $element_parent_parent_id, !4 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   BIND_GLOBAL                                              !0, 'catobj'
   22     1        ASSIGN                                                   !0, <array>
   24     2        NEW                                              $7      'simulating_categories_object'
          3        SEND_VAL_EX                                              1
          4        SEND_VAL_EX                                              0
          5        SEND_VAL_EX                                              'domain1'
          6        DO_FCALL                                      0          
          7        ASSIGN_DIM                                               !0
          8        OP_DATA                                                  $7
   25     9        NEW                                              $10     'simulating_categories_object'
         10        SEND_VAL_EX                                              2
         11        SEND_VAL_EX                                              0
         12        SEND_VAL_EX                                              'domain2'
         13        DO_FCALL                                      0          
         14        ASSIGN_DIM                                               !0
         15        OP_DATA                                                  $10
   26    16        NEW                                              $13     'simulating_categories_object'
         17        SEND_VAL_EX                                              3
         18        SEND_VAL_EX                                              0
         19        SEND_VAL_EX                                              'domain3'
         20        DO_FCALL                                      0          
         21        ASSIGN_DIM                                               !0
         22        OP_DATA                                                  $13
   28    23        NEW                                              $16     'simulating_categories_object'
         24        SEND_VAL_EX                                              4
         25        SEND_VAL_EX                                              1
         26        SEND_VAL_EX                                              'category1'
         27        DO_FCALL                                      0          
         28        ASSIGN_DIM                                               !0
         29        OP_DATA                                                  $16
   29    30        NEW                                              $19     'simulating_categories_object'
         31        SEND_VAL_EX                                              5
         32        SEND_VAL_EX                                              1
         33        SEND_VAL_EX                                              'category2'
         34        DO_FCALL                                      0          
         35        ASSIGN_DIM                                               !0
         36        OP_DATA                                                  $19
   30    37        NEW                                              $22     'simulating_categories_object'
         38        SEND_VAL_EX                                              6
         39        SEND_VAL_EX                                              1
         40        SEND_VAL_EX                                              'category3'
         41        DO_FCALL                                      0          
         42        ASSIGN_DIM                                               !0
         43        OP_DATA                                                  $22
   32    44        NEW                                              $25     'simulating_categories_object'
         45        SEND_VAL_EX                                              7
         46        SEND_VAL_EX                                              4
         47        SEND_VAL_EX                                              'subcategory1'
         48        DO_FCALL                                      0          
         49        ASSIGN_DIM                                               !0
         50        OP_DATA                                                  $25
   33    51        NEW                                              $28     'simulating_categories_object'
         52        SEND_VAL_EX                                              8
         53        SEND_VAL_EX                                              4
         54        SEND_VAL_EX                                              'subcategory2'
         55        DO_FCALL                                      0          
         56        ASSIGN_DIM                                               !0
         57        OP_DATA                                                  $28
   35    58        NEW                                              $31     'simulating_categories_object'
         59        SEND_VAL_EX                                              9
         60        SEND_VAL_EX                                              2
         61        SEND_VAL_EX                                              'category555'
         62        DO_FCALL                                      0          
         63        ASSIGN_DIM                                               !0
         64        OP_DATA                                                  $31
   36    65        NEW                                              $34     'simulating_categories_object'
         66        SEND_VAL_EX                                              10
         67        SEND_VAL_EX                                              9
         68        SEND_VAL_EX                                              'subcategory666'
         69        DO_FCALL                                      0          
         70        ASSIGN_DIM                                               !0
         71        OP_DATA                                                  $34
   38    72        NEW                                              $37     'simulating_categories_object'
         73        SEND_VAL_EX                                              11
         74        SEND_VAL_EX                                              3
         75        SEND_VAL_EX                                              'category777'
         76        DO_FCALL                                      0          
         77        ASSIGN_DIM                                               !0
         78        OP_DATA                                                  $37
   50    79        ECHO                                                     '%3Cpre%3E'
   52    80        ECHO                                                     '%3C%2Fpre%3E'
   54    81      > FE_RESET_R                                       $39     !0, ->114
         82    > > FE_FETCH_R                                               $39, !1, ->114
   56    83    >   FETCH_OBJ_R                                      ~40     !1, 'parent'
         84        IS_EQUAL                                                 ~40, 0
         85      > JMPZ                                                     ~41, ->91
   58    86    >   FETCH_OBJ_R                                      ~42     !1, 'slug'
         87        CONCAT                                           ~43     'This+element+is+a+domain.+The+slug+of+the+element+is+', ~42
         88        CONCAT                                           ~44     ~43, '%3Cbr%3E%0A'
         89        ECHO                                                     ~44
   59    90      > JMP                                                      ->82
   62    91    >   INIT_FCALL                                               'get_the_parent_id_of_a_given_element_id'
         92        FETCH_OBJ_R                                      ~45     !1, 'parent'
         93        SEND_VAL                                                 ~45
         94        DO_FCALL                                      0  $46     
         95        ASSIGN                                                   !2, $46
   64    96        IS_EQUAL                                                 !2, 0
         97      > JMPZ                                                     ~48, ->103
   66    98    >   FETCH_OBJ_R                                      ~49     !1, 'slug'
         99        CONCAT                                           ~50     'This+element+is+a+category.+The+slug+of+the+element+is+', ~49
        100        CONCAT                                           ~51     ~50, '%3Cbr%3E%0A'
        101        ECHO                                                     ~51
   67   102      > JMP                                                      ->82
   70   103    >   INIT_FCALL                                               'get_the_parent_id_of_a_given_element_id'
        104        SEND_VAR                                                 !2
        105        DO_FCALL                                      0  $52     
        106        ASSIGN                                                   !3, $52
   72   107        IS_EQUAL                                                 !3, 0
        108      > JMPZ                                                     ~54, ->113
   74   109    >   FETCH_OBJ_R                                      ~55     !1, 'slug'
        110        CONCAT                                           ~56     'This+element+is+a+subcategory.+The+slug+of+the+element+is+', ~55
        111        CONCAT                                           ~57     ~56, '%3Cbr%3E%0A'
        112        ECHO                                                     ~57
   54   113    > > JMP                                                      ->82
        114    >   FE_FREE                                                  $39
   79   115        ASSIGN                                                   !4, <array>
   93   116        ECHO                                                     '%3Cpre%3E'
   94   117        INIT_FCALL                                               'print_r'
        118        SEND_VAR                                                 !4
        119        DO_ICALL                                                 
   95   120        ECHO                                                     '%3C%2Fpre%3E'
        121      > RETURN                                                   1

Function get_the_parent_id_of_a_given_element_id:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/Gm6X1
function name:  get_the_parent_id_of_a_given_element_id
number of ops:  13
compiled vars:  !0 = $id, !1 = $catobj, !2 = $category
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   42     1        BIND_GLOBAL                                              !1, 'catobj'
   43     2      > FE_RESET_R                                       $3      !1, ->11
          3    > > FE_FETCH_R                                               $3, !2, ->11
   44     4    >   FETCH_OBJ_R                                      ~4      !2, 'id'
          5        IS_EQUAL                                                 !0, ~4
          6      > JMPZ                                                     ~5, ->10
   45     7    >   FETCH_OBJ_R                                      ~6      !2, 'parent'
          8        FE_FREE                                                  $3
          9      > RETURN                                                   ~6
   43    10    > > JMP                                                      ->3
         11    >   FE_FREE                                                  $3
   47    12      > RETURN                                                   null

End of function get_the_parent_id_of_a_given_element_id

Class simulating_categories_object:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Gm6X1
function name:  __construct
number of ops:  10
compiled vars:  !0 = $id, !1 = $parent, !2 = $slug
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   15     3        ASSIGN_OBJ                                               'id'
          4        OP_DATA                                                  !0
   16     5        ASSIGN_OBJ                                               'parent'
          6        OP_DATA                                                  !1
   17     7        ASSIGN_OBJ                                               'slug'
          8        OP_DATA                                                  !2
   18     9      > RETURN                                                   null

End of function __construct

End of class simulating_categories_object.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
165.18 ms | 1411 KiB | 17 Q