3v4l.org

run code in 300+ PHP versions simultaneously
<?php $startingArray = [ [ "subject" => "Physics", "student" => [ ["id" => "00003", "Name" => "Peter", "email" => "peter@schooool.com",], ["id" => "00004", "Name" => "Mary", "email" => "mary@schooool.com",], ["id" => "00005", "Name" => "Jane", "email" => "jane@schooool.com",], ] ], [ "subject" => "Chemistry", "student" => [ ["id" => "00003", "Name" => "Peter", "email" => "peter@schooool.com",], ["id" => "00004", "Name" => "Mary", "email" => "mary@schooool.com",], ["id" => "00005", "Name" => "Jane", "email" => "jane@schooool.com",], ] ], [ "subject" => "Mathematics", "student" => [ ["id" => "00003", "Name" => "Peter", "email" => "peter@schooool.com",], ["id" => "00006", "Name" => "Fred", "email" => "fred@schooool.com",], ["id" => "00007", "Name" => "Wilma", "email" => "wilma@schooool.com",], ] ], [ "subject" => "Biology", "student" => [ ["id" => "00004", "Name" => "Mary", "email" => "mary@schooool.com",], ["id" => "00006", "Name" => "Fred", "email" => "fred@schooool.com",], ["id" => "00008", "Name" => "Alison", "email" => "alison@schooool.com",], ] ] ]; $newArray = []; foreach ($startingArray as $item) { foreach ($item['student'] as $student) { if (empty($newArray[$student['id']])) { $student['subjects'] = []; $newArray[$student['id']] = $student; } $newArray[$student['id']]['subjects'][] = $item['subject']; } } print_r($newArray);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 24
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 24
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 22
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 22
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 15
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 15
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 22
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/dR6L3
function name:  (null)
number of ops:  29
compiled vars:  !0 = $startingArray, !1 = $newArray, !2 = $item, !3 = $student
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   34     1        ASSIGN                                                   !1, <array>
   35     2      > FE_RESET_R                                       $6      !0, ->24
          3    > > FE_FETCH_R                                               $6, !2, ->24
   36     4    >   FETCH_DIM_R                                      ~7      !2, 'student'
          5      > FE_RESET_R                                       $8      ~7, ->22
          6    > > FE_FETCH_R                                               $8, !3, ->22
   37     7    >   FETCH_DIM_R                                      ~9      !3, 'id'
          8        ISSET_ISEMPTY_DIM_OBJ                         1          !1, ~9
          9      > JMPZ                                                     ~10, ->15
   38    10    >   ASSIGN_DIM                                               !3, 'subjects'
         11        OP_DATA                                                  <array>
   39    12        FETCH_DIM_R                                      ~12     !3, 'id'
         13        ASSIGN_DIM                                               !1, ~12
         14        OP_DATA                                                  !3
   42    15    >   FETCH_DIM_R                                      ~14     !3, 'id'
         16        FETCH_DIM_R                                      ~18     !2, 'subject'
         17        FETCH_DIM_W                                      $15     !1, ~14
         18        FETCH_DIM_W                                      $16     $15, 'subjects'
         19        ASSIGN_DIM                                               $16
         20        OP_DATA                                                  ~18
   36    21      > JMP                                                      ->6
         22    >   FE_FREE                                                  $8
   35    23      > JMP                                                      ->3
         24    >   FE_FREE                                                  $6
   46    25        INIT_FCALL                                               'print_r'
         26        SEND_VAR                                                 !1
         27        DO_ICALL                                                 
         28      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
126.55 ms | 1014 KiB | 14 Q