3v4l.org

run code in 300+ PHP versions simultaneously
<?php $ori=json_decode(<<<JSON [{"category":"ABC","orderID":"12345","Name":"Pen","Name2":"Black"}, {"category":"ABC","orderID":"34545","Name":"Pencil","Name2":"White"}, {"category":"ABC","orderID":"34545","Name":"Pen","Name2":"Black"}, {"category":"DEF","orderID":"12345","Name":"Pencil","Name2":"Black"}, {"category":"DEF","orderID":"12345","Name":"Pen","Name2":"White"}] JSON ,true); $result=array(); while($row=array_shift($ori))//Pretend that we're fetching record from DB { $result[$row["category"]][$row["orderID"]][]=array("Name"=>$row["Name"],"Name2"=>$row["Name2"]); } print_r($result);//This should be better //But if you insist, this can help: array_walk($result,function(&$v,$k){ $x=array("category"=>$k,"children"=>array()); array_walk($v,function($arr,$oid)use(&$x){ $x["children"][]=array("orderID"=>$oid,"children"=>$arr); }); $v=$x; }); print_r(array_values($result));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 7
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 7
Branch analysis from position: 22
Branch analysis from position: 7
filename:       /in/abWvg
function name:  (null)
number of ops:  37
compiled vars:  !0 = $ori, !1 = $result, !2 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'json_decode'
    3     1        SEND_VAL                                                 '%5B%7B%22category%22%3A%22ABC%22%2C%22orderID%22%3A%2212345%22%2C%22Name%22%3A%22Pen%22%2C%22Name2%22%3A%22Black%22%7D%2C%0A%7B%22category%22%3A%22ABC%22%2C%22orderID%22%3A%2234545%22%2C%22Name%22%3A%22Pencil%22%2C%22Name2%22%3A%22White%22%7D%2C%0A%7B%22category%22%3A%22ABC%22%2C%22orderID%22%3A%2234545%22%2C%22Name%22%3A%22Pen%22%2C%22Name2%22%3A%22Black%22%7D%2C%0A%7B%22category%22%3A%22DEF%22%2C%22orderID%22%3A%2212345%22%2C%22Name%22%3A%22Pencil%22%2C%22Name2%22%3A%22Black%22%7D%2C%0A%7B%22category%22%3A%22DEF%22%2C%22orderID%22%3A%2212345%22%2C%22Name%22%3A%22Pen%22%2C%22Name2%22%3A%22White%22%7D%5D'
    9     2        SEND_VAL                                                 <true>
          3        DO_ICALL                                         $3      
    2     4        ASSIGN                                                   !0, $3
   10     5        ASSIGN                                                   !1, <array>
   11     6      > JMP                                                      ->17
   13     7    >   FETCH_DIM_R                                      ~6      !2, 'category'
          8        FETCH_DIM_R                                      ~8      !2, 'orderID'
          9        FETCH_DIM_R                                      ~11     !2, 'Name'
         10        INIT_ARRAY                                       ~12     ~11, 'Name'
         11        FETCH_DIM_R                                      ~13     !2, 'Name2'
         12        ADD_ARRAY_ELEMENT                                ~12     ~13, 'Name2'
         13        FETCH_DIM_W                                      $7      !1, ~6
         14        FETCH_DIM_W                                      $9      $7, ~8
         15        ASSIGN_DIM                                               $9
         16        OP_DATA                                                  ~12
   11    17    >   INIT_FCALL                                               'array_shift'
         18        SEND_REF                                                 !0
         19        DO_ICALL                                         $14     
         20        ASSIGN                                           ~15     !2, $14
         21      > JMPNZ                                                    ~15, ->7
   15    22    >   INIT_FCALL                                               'print_r'
         23        SEND_VAR                                                 !1
         24        DO_ICALL                                                 
   17    25        INIT_FCALL                                               'array_walk'
         26        SEND_REF                                                 !1
         27        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FabWvg%3A17%240'
   23    28        SEND_VAL                                                 ~17
         29        DO_ICALL                                                 
   24    30        INIT_FCALL                                               'print_r'
         31        INIT_FCALL                                               'array_values'
         32        SEND_VAR                                                 !1
         33        DO_ICALL                                         $19     
         34        SEND_VAR                                                 $19
         35        DO_ICALL                                                 
         36      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FabWvg%3A17%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/abWvg
function name:  {closure}
number of ops:  13
compiled vars:  !0 = $v, !1 = $k, !2 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        INIT_ARRAY                                       ~3      !1, 'category'
          3        ADD_ARRAY_ELEMENT                                ~3      <array>, 'children'
          4        ASSIGN                                                   !2, ~3
   19     5        INIT_FCALL                                               'array_walk'
          6        SEND_REF                                                 !0
          7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FabWvg%3A19%241'
          8        BIND_LEXICAL                                             ~5, !2
   21     9        SEND_VAL                                                 ~5
         10        DO_ICALL                                                 
   22    11        ASSIGN                                                   !0, !2
   23    12      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FabWvg%3A17%240

Function %00%7Bclosure%7D%2Fin%2FabWvg%3A19%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/abWvg
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $arr, !1 = $oid, !2 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   20     3        INIT_ARRAY                                       ~5      !1, 'orderID'
          4        ADD_ARRAY_ELEMENT                                ~5      !0, 'children'
          5        FETCH_DIM_W                                      $3      !2, 'children'
          6        ASSIGN_DIM                                               $3
          7        OP_DATA                                                  ~5
   21     8      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FabWvg%3A19%241

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.26 ms | 1400 KiB | 23 Q