3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '{"array":[{"Name": "John Doe", "mail": "john-doe@gmail.com"}, {"Name": "Alex Smith", "mail": "alex-smith@gmx.com"}]}'; $target = json_decode($json, true); $insert = array("Name"=>"Thomas Dover", "mail"=>"thomas-dover@icloud.com"); // Temporary array $between = []; // Flag for if/when we find the first person $foundStart = false; foreach($target['array'] as $key => $v) { // If we found the first person previously, and this is our second person if($foundStart && 'Alex Smith' === $v['Name']) { // Insert $between[] = $insert; // Reset the flag just in case the second person is found again $foundStart = false; }elseif('John Doe' === $v['Name']){ // Flag that we found the first person $foundStart = true; } // No matter what, append our current item to the temp array $between[] = $v; } // Overwrite our original loop's variable with our temporary $target['array'] = $between; echo json_encode($target);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 29
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 29
Branch analysis from position: 12
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 22
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 26
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 26
Branch analysis from position: 17
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
filename:       /in/2KPIb
function name:  (null)
number of ops:  37
compiled vars:  !0 = $json, !1 = $target, !2 = $insert, !3 = $between, !4 = $foundStart, !5 = $v, !6 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%7B%22array%22%3A%5B%7B%22Name%22%3A+%22John+Doe%22%2C+%22mail%22%3A+%22john-doe%40gmail.com%22%7D%2C+%7B%22Name%22%3A+%22Alex+Smith%22%2C+%22mail%22%3A+%22alex-smith%40gmx.com%22%7D%5D%7D'
    5     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $8      
          5        ASSIGN                                                   !1, $8
    7     6        ASSIGN                                                   !2, <array>
   10     7        ASSIGN                                                   !3, <array>
   13     8        ASSIGN                                                   !4, <false>
   15     9        FETCH_DIM_R                                      ~13     !1, 'array'
         10      > FE_RESET_R                                       $14     ~13, ->29
         11    > > FE_FETCH_R                                       ~15     $14, !5, ->29
         12    >   ASSIGN                                                   !6, ~15
   18    13      > JMPZ_EX                                          ~17     !4, ->17
         14    >   FETCH_DIM_R                                      ~18     !5, 'Name'
         15        IS_IDENTICAL                                     ~19     ~18, 'Alex+Smith'
         16        BOOL                                             ~17     ~19
         17    > > JMPZ                                                     ~17, ->22
   20    18    >   ASSIGN_DIM                                               !3
         19        OP_DATA                                                  !2
   23    20        ASSIGN                                                   !4, <false>
   18    21      > JMP                                                      ->26
   24    22    >   FETCH_DIM_R                                      ~22     !5, 'Name'
         23        IS_IDENTICAL                                             ~22, 'John+Doe'
         24      > JMPZ                                                     ~23, ->26
   27    25    >   ASSIGN                                                   !4, <true>
   31    26    >   ASSIGN_DIM                                               !3
         27        OP_DATA                                                  !5
   15    28      > JMP                                                      ->11
         29    >   FE_FREE                                                  $14
   35    30        ASSIGN_DIM                                               !1, 'array'
         31        OP_DATA                                                  !3
   37    32        INIT_FCALL                                               'json_encode'
         33        SEND_VAR                                                 !1
         34        DO_ICALL                                         $27     
         35        ECHO                                                     $27
         36      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.76 ms | 1006 KiB | 15 Q