3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A1 { public function rows() { $a = array(); $a[] = array('parent_id' => 10, 'category_id' => 13, 'name' => 'id13', 'sort_order' => 7); $a[] = array('parent_id' => 8, 'category_id' => 12, 'name' => 'id12', 'sort_order' => 6); $a[] = array('parent_id' => 7, 'category_id' => 11, 'name' => 'id11', 'sort_order' => 5); $a[] = array('parent_id' => 7, 'category_id' => 10, 'name' => 'id10', 'sort_order' => 4); $a[] = array('parent_id' => 0, 'category_id' => 9, 'name' => 'id9', 'sort_order' => 3); $a[] = array('parent_id' => 0, 'category_id' => 8, 'name' => 'id8', 'sort_order' => 2); $a[] = array('parent_id' => 0, 'category_id' => 7, 'name' => 'id7', 'sort_order' => 1); return $a; } } $query = new A1; $categoryArray = array(); foreach($query->rows() as $row) { if(isset($categoryArray[$row['category_id']])) { $categoryArray[$row['parent_id']][$row['category_id']]['name'] = $row['name']; $categoryArray[$row['parent_id']][$row['category_id']]['elements'] = $categoryArray[$row['category_id']]; unset($categoryArray[$row['category_id']]); } else { $categoryArray[$row['parent_id']][$row['category_id']]['name'] = $row['name']; } } ksort($categoryArray[0]); echo "first stage \n"; print_r($categoryArray); // second stage echo "\n\n"; getCategory($categoryArray[0]); function getCategory($array){ foreach($array as $row){ if(isset($row['elements'])) { echo('<li>'.$row['name'].'<ul>'); getCategory(ksort($row['elements'])); echo('</ul></li>'); } else { echo('<li>'.$row['name'].'</li>'); } } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 37
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 37
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 29
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename:       /in/WJvGm
function name:  (null)
number of ops:  53
compiled vars:  !0 = $query, !1 = $categoryArray, !2 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   NEW                                              $3      'A1'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $3
   19     3        ASSIGN                                                   !1, <array>
   21     4        INIT_METHOD_CALL                                         !0, 'rows'
          5        DO_FCALL                                      0  $7      
          6      > FE_RESET_R                                       $8      $7, ->37
          7    > > FE_FETCH_R                                               $8, !2, ->37
   22     8    >   FETCH_DIM_R                                      ~9      !2, 'category_id'
          9        ISSET_ISEMPTY_DIM_OBJ                         0          !1, ~9
         10      > JMPZ                                                     ~10, ->29
   23    11    >   FETCH_DIM_R                                      ~11     !2, 'parent_id'
         12        FETCH_DIM_R                                      ~13     !2, 'category_id'
         13        FETCH_DIM_R                                      ~16     !2, 'name'
         14        FETCH_DIM_W                                      $12     !1, ~11
         15        FETCH_DIM_W                                      $14     $12, ~13
         16        ASSIGN_DIM                                               $14, 'name'
         17        OP_DATA                                                  ~16
   24    18        FETCH_DIM_R                                      ~17     !2, 'parent_id'
         19        FETCH_DIM_R                                      ~19     !2, 'category_id'
         20        FETCH_DIM_R                                      ~22     !2, 'category_id'
         21        FETCH_DIM_R                                      ~23     !1, ~22
         22        FETCH_DIM_W                                      $18     !1, ~17
         23        FETCH_DIM_W                                      $20     $18, ~19
         24        ASSIGN_DIM                                               $20, 'elements'
         25        OP_DATA                                                  ~23
   25    26        FETCH_DIM_R                                      ~24     !2, 'category_id'
         27        UNSET_DIM                                                !1, ~24
         28      > JMP                                                      ->36
   27    29    >   FETCH_DIM_R                                      ~25     !2, 'parent_id'
         30        FETCH_DIM_R                                      ~27     !2, 'category_id'
         31        FETCH_DIM_R                                      ~30     !2, 'name'
         32        FETCH_DIM_W                                      $26     !1, ~25
         33        FETCH_DIM_W                                      $28     $26, ~27
         34        ASSIGN_DIM                                               $28, 'name'
         35        OP_DATA                                                  ~30
   21    36    > > JMP                                                      ->7
         37    >   FE_FREE                                                  $8
   31    38        INIT_FCALL                                               'ksort'
         39        FETCH_DIM_W                                      $31     !1, 0
         40        SEND_REF                                                 $31
         41        DO_ICALL                                                 
   32    42        ECHO                                                     'first+stage+%0A'
   33    43        INIT_FCALL                                               'print_r'
         44        SEND_VAR                                                 !1
         45        DO_ICALL                                                 
   37    46        ECHO                                                     '%0A%0A'
   38    47        INIT_FCALL_BY_NAME                                       'getCategory'
         48        CHECK_FUNC_ARG                                           
         49        FETCH_DIM_FUNC_ARG                               $34     !1, 0
         50        SEND_FUNC_ARG                                            $34
         51        DO_FCALL                                      0          
   50    52      > RETURN                                                   1

Function getcategory:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 23
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 23
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 18
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/WJvGm
function name:  getCategory
number of ops:  25
compiled vars:  !0 = $array, !1 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   41     1      > FE_RESET_R                                       $2      !0, ->23
          2    > > FE_FETCH_R                                               $2, !1, ->23
   42     3    >   ISSET_ISEMPTY_DIM_OBJ                         0          !1, 'elements'
          4      > JMPZ                                                     ~3, ->18
   43     5    >   FETCH_DIM_R                                      ~4      !1, 'name'
          6        CONCAT                                           ~5      '%3Cli%3E', ~4
          7        CONCAT                                           ~6      ~5, '%3Cul%3E'
          8        ECHO                                                     ~6
   44     9        INIT_FCALL_BY_NAME                                       'getCategory'
         10        INIT_FCALL                                               'ksort'
         11        FETCH_DIM_W                                      $7      !1, 'elements'
         12        SEND_REF                                                 $7
         13        DO_ICALL                                         $8      
         14        SEND_VAR_NO_REF_EX                                       $8
         15        DO_FCALL                                      0          
   45    16        ECHO                                                     '%3C%2Ful%3E%3C%2Fli%3E'
         17      > JMP                                                      ->22
   47    18    >   FETCH_DIM_R                                      ~10     !1, 'name'
         19        CONCAT                                           ~11     '%3Cli%3E', ~10
         20        CONCAT                                           ~12     ~11, '%3C%2Fli%3E'
         21        ECHO                                                     ~12
   41    22    > > JMP                                                      ->2
         23    >   FE_FREE                                                  $2
   50    24      > RETURN                                                   null

End of function getcategory

Class A1:
Function rows:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WJvGm
function name:  rows
number of ops:  17
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ASSIGN                                                   !0, <array>
    6     1        ASSIGN_DIM                                               !0
          2        OP_DATA                                                  <array>
    7     3        ASSIGN_DIM                                               !0
          4        OP_DATA                                                  <array>
    8     5        ASSIGN_DIM                                               !0
          6        OP_DATA                                                  <array>
    9     7        ASSIGN_DIM                                               !0
          8        OP_DATA                                                  <array>
   10     9        ASSIGN_DIM                                               !0
         10        OP_DATA                                                  <array>
   11    11        ASSIGN_DIM                                               !0
         12        OP_DATA                                                  <array>
   12    13        ASSIGN_DIM                                               !0
         14        OP_DATA                                                  <array>
   14    15      > RETURN                                                   !0
   15    16*     > RETURN                                                   null

End of function rows

End of class A1.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.02 ms | 1404 KiB | 17 Q