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"); // We will overwrite $val later so we want a reference foreach($target as $x => &$val) { // Temporary array $between = []; // Flag for if/when we find the first person $foundStart = false; foreach($val 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 $val = $between; } // Clean up reference variable so surprises don't happen unset($val); echo json_encode($target);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 8, Position 2 = 34
Branch analysis from position: 8
2 jumps found. (Code = 126) Position 1 = 9, Position 2 = 34
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 31
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 31
Branch analysis from position: 14
2 jumps found. (Code = 46) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 24
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 28
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 28
Branch analysis from position: 19
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 31
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
filename:       /in/DnJht
function name:  (null)
number of ops:  41
compiled vars:  !0 = $json, !1 = $target, !2 = $insert, !3 = $val, !4 = $x, !5 = $between, !6 = $foundStart, !7 = $v, !8 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     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'
    4     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $10     
          5        ASSIGN                                                   !1, $10
    6     6        ASSIGN                                                   !2, <array>
    9     7      > FE_RESET_RW                                      $13     !1, ->34
          8    > > FE_FETCH_RW                                      ~14     $13, !3, ->34
          9    >   ASSIGN                                                   !4, ~14
   12    10        ASSIGN                                                   !5, <array>
   15    11        ASSIGN                                                   !6, <false>
   17    12      > FE_RESET_R                                       $18     !3, ->31
         13    > > FE_FETCH_R                                       ~19     $18, !7, ->31
         14    >   ASSIGN                                                   !8, ~19
   20    15      > JMPZ_EX                                          ~21     !6, ->19
         16    >   FETCH_DIM_R                                      ~22     !7, 'Name'
         17        IS_IDENTICAL                                     ~23     ~22, 'Alex+Smith'
         18        BOOL                                             ~21     ~23
         19    > > JMPZ                                                     ~21, ->24
   22    20    >   ASSIGN_DIM                                               !5
         21        OP_DATA                                                  !2
   25    22        ASSIGN                                                   !6, <false>
   20    23      > JMP                                                      ->28
   26    24    >   FETCH_DIM_R                                      ~26     !7, 'Name'
         25        IS_IDENTICAL                                             ~26, 'John+Doe'
         26      > JMPZ                                                     ~27, ->28
   29    27    >   ASSIGN                                                   !6, <true>
   33    28    >   ASSIGN_DIM                                               !5
         29        OP_DATA                                                  !7
   17    30      > JMP                                                      ->13
         31    >   FE_FREE                                                  $18
   37    32        ASSIGN                                                   !3, !5
    9    33      > JMP                                                      ->8
         34    >   FE_FREE                                                  $13
   41    35        UNSET_CV                                                 !3
   43    36        INIT_FCALL                                               'json_encode'
         37        SEND_VAR                                                 !1
         38        DO_ICALL                                         $31     
         39        ECHO                                                     $31
         40      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.44 ms | 1006 KiB | 15 Q