3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = '[{"id":"1","title":"Title 1","answer":"True"},{"id":"1","title":"Title 1","answer":"False"}]'; $data = json_decode($data); $data2=array(); $count=0; foreach($data as $l){ if ($count % 2 == 0){ $data2[$count]=array( "id" =>$l->id, "title"=>$l->title, ); $data2[$count]['answer'] = []; $data2[$count]['answer'][] = $l->answer; } else { $data2[$count-1]['answer'][] = $l->answer; } $count++; } print_r(json_encode($data2));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 35
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 35
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 27
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
filename:       /in/u07uV
function name:  (null)
number of ops:  43
compiled vars:  !0 = $data, !1 = $data2, !2 = $count, !3 = $l
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%5B%7B%22id%22%3A%221%22%2C%22title%22%3A%22Title+1%22%2C%22answer%22%3A%22True%22%7D%2C%7B%22id%22%3A%221%22%2C%22title%22%3A%22Title+1%22%2C%22answer%22%3A%22False%22%7D%5D'
    5     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $5      
          4        ASSIGN                                                   !0, $5
    6     5        ASSIGN                                                   !1, <array>
    7     6        ASSIGN                                                   !2, 0
    8     7      > FE_RESET_R                                       $9      !0, ->35
          8    > > FE_FETCH_R                                               $9, !3, ->35
    9     9    >   MOD                                              ~10     !2, 2
         10        IS_EQUAL                                                 ~10, 0
         11      > JMPZ                                                     ~11, ->27
   11    12    >   FETCH_OBJ_R                                      ~13     !3, 'id'
         13        INIT_ARRAY                                       ~14     ~13, 'id'
   12    14        FETCH_OBJ_R                                      ~15     !3, 'title'
         15        ADD_ARRAY_ELEMENT                                ~14     ~15, 'title'
   10    16        ASSIGN_DIM                                               !1, !2
   12    17        OP_DATA                                                  ~14
   15    18        FETCH_DIM_W                                      $16     !1, !2
         19        ASSIGN_DIM                                               $16, 'answer'
         20        OP_DATA                                                  <array>
   16    21        FETCH_OBJ_R                                      ~21     !3, 'answer'
         22        FETCH_DIM_W                                      $18     !1, !2
         23        FETCH_DIM_W                                      $19     $18, 'answer'
         24        ASSIGN_DIM                                               $19
         25        OP_DATA                                                  ~21
    9    26      > JMP                                                      ->33
   18    27    >   SUB                                              ~22     !2, 1
         28        FETCH_OBJ_R                                      ~26     !3, 'answer'
         29        FETCH_DIM_W                                      $23     !1, ~22
         30        FETCH_DIM_W                                      $24     $23, 'answer'
         31        ASSIGN_DIM                                               $24
         32        OP_DATA                                                  ~26
   21    33    >   PRE_INC                                                  !2
    8    34      > JMP                                                      ->8
         35    >   FE_FREE                                                  $9
   24    36        INIT_FCALL                                               'print_r'
         37        INIT_FCALL                                               'json_encode'
         38        SEND_VAR                                                 !1
         39        DO_ICALL                                         $28     
         40        SEND_VAR                                                 $28
         41        DO_ICALL                                                 
         42      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.15 ms | 1009 KiB | 16 Q