3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = Array ( 0 => Array ( 'template_id' => 1, 'template_field_id' => 20 ), 1 => Array ( 'template_id' => 1, 'template_field_id' => 21 ), 2 => Array ( 'template_id' => 2, 'template_field_id' => 22 ), 3 => Array ( 'template_id' => 2, 'template_field_id' => 23 ) ); $resultArr = array(); foreach($arr as $data){ $index = array_search($data['template_id'], array_column($resultArr, 'id')); if($index !== false){ if(is_array($resultArr[$index]["template_fields"])) array_push($resultArr[$index]["template_fields"],array("id"=>$data["template_field_id"])); else $resultArr[$index]["template_fields"] = array(array("id"=>$data["template_field_id"])); }else{ $newData = array("id"=>$data['template_id'],"template_fields"=>array(array("id"=>$data["template_field_id"]))); array_push($resultArr,$newData); } } print_r($resultArr);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 48
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 48
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 36
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 29
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
filename:       /in/f5Kb0
function name:  (null)
number of ops:  53
compiled vars:  !0 = $arr, !1 = $resultArr, !2 = $data, !3 = $index, !4 = $newData
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   30     1        ASSIGN                                                   !1, <array>
   31     2      > FE_RESET_R                                       $7      !0, ->48
          3    > > FE_FETCH_R                                               $7, !2, ->48
   32     4    >   INIT_FCALL                                               'array_search'
          5        FETCH_DIM_R                                      ~8      !2, 'template_id'
          6        SEND_VAL                                                 ~8
          7        INIT_FCALL                                               'array_column'
          8        SEND_VAR                                                 !1
          9        SEND_VAL                                                 'id'
         10        DO_ICALL                                         $9      
         11        SEND_VAR                                                 $9
         12        DO_ICALL                                         $10     
         13        ASSIGN                                                   !3, $10
   33    14        TYPE_CHECK                                  1018          !3
         15      > JMPZ                                                     ~12, ->36
   34    16    >   FETCH_DIM_R                                      ~13     !1, !3
         17        FETCH_DIM_R                                      ~14     ~13, 'template_fields'
         18        TYPE_CHECK                                  128          ~14
         19      > JMPZ                                                     ~15, ->29
   35    20    >   INIT_FCALL                                               'array_push'
         21        FETCH_DIM_W                                      $16     !1, !3
         22        FETCH_DIM_W                                      $17     $16, 'template_fields'
         23        SEND_REF                                                 $17
         24        FETCH_DIM_R                                      ~18     !2, 'template_field_id'
         25        INIT_ARRAY                                       ~19     ~18, 'id'
         26        SEND_VAL                                                 ~19
         27        DO_ICALL                                                 
   34    28      > JMP                                                      ->35
   37    29    >   FETCH_DIM_R                                      ~23     !2, 'template_field_id'
         30        INIT_ARRAY                                       ~24     ~23, 'id'
         31        INIT_ARRAY                                       ~25     ~24
         32        FETCH_DIM_W                                      $21     !1, !3
         33        ASSIGN_DIM                                               $21, 'template_fields'
         34        OP_DATA                                                  ~25
   33    35    > > JMP                                                      ->47
   40    36    >   FETCH_DIM_R                                      ~26     !2, 'template_id'
         37        INIT_ARRAY                                       ~27     ~26, 'id'
         38        FETCH_DIM_R                                      ~28     !2, 'template_field_id'
         39        INIT_ARRAY                                       ~29     ~28, 'id'
         40        INIT_ARRAY                                       ~30     ~29
         41        ADD_ARRAY_ELEMENT                                ~27     ~30, 'template_fields'
         42        ASSIGN                                                   !4, ~27
   41    43        INIT_FCALL                                               'array_push'
         44        SEND_REF                                                 !1
         45        SEND_VAR                                                 !4
         46        DO_ICALL                                                 
   31    47    > > JMP                                                      ->3
         48    >   FE_FREE                                                  $7
   45    49        INIT_FCALL                                               'print_r'
         50        SEND_VAR                                                 !1
         51        DO_ICALL                                                 
         52      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.26 ms | 1018 KiB | 17 Q