3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = <<<EOD [ { "id": "3", "type": "default", "place": "1" }, { "id": "3", "type": "default", "place": "2" }, { "id": "3", "type": "default", "place": "3" } ] EOD; $array = json_decode($json, true); // Here is trick; get the key of the array using array_column $key = array_search('2', array_column($array, 'place')); // If any key found modify the array if ($key && array_key_exists($key, $array)) { $array[$key] = [ 'id' => 7, 'type' => 1, 'place' => 2, 'description' => 'This is item place 2', 'image' => 'this_is_the_image.png', ]; } print_r($array);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 21
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
Branch analysis from position: 18
filename:       /in/cbIvP
function name:  (null)
number of ops:  25
compiled vars:  !0 = $json, !1 = $array, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%5B%0A++++%7B%0A++++++++%22id%22%3A+%223%22%2C%0A++++++++%22type%22%3A+%22default%22%2C%0A++++++++%22place%22%3A+%221%22%0A++++%7D%2C%0A++++%7B%0A++++++++%22id%22%3A+%223%22%2C%0A++++++++%22type%22%3A+%22default%22%2C%0A++++++++%22place%22%3A+%222%22%0A++++%7D%2C%0A++++%7B%0A++++++++%22id%22%3A+%223%22%2C%0A++++++++%22type%22%3A+%22default%22%2C%0A++++++++%22place%22%3A+%223%22%0A++++%7D%0A%5D'
   23     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
   26     6        INIT_FCALL                                               'array_search'
          7        SEND_VAL                                                 '2'
          8        INIT_FCALL                                               'array_column'
          9        SEND_VAR                                                 !1
         10        SEND_VAL                                                 'place'
         11        DO_ICALL                                         $6      
         12        SEND_VAR                                                 $6
         13        DO_ICALL                                         $7      
         14        ASSIGN                                                   !2, $7
   29    15      > JMPZ_EX                                          ~9      !2, ->18
         16    >   ARRAY_KEY_EXISTS                                 ~10     !2, !1
         17        BOOL                                             ~9      ~10
         18    > > JMPZ                                                     ~9, ->21
   30    19    >   ASSIGN_DIM                                               !1, !2
   31    20        OP_DATA                                                  <array>
   39    21    >   INIT_FCALL                                               'print_r'
         22        SEND_VAR                                                 !1
         23        DO_ICALL                                                 
         24      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
239.36 ms | 1004 KiB | 17 Q