3v4l.org

run code in 500+ PHP versions simultaneously
<?php $json = '[{ "id": 1, "time": "2018-12-16", "pages_indexed": 1024 }, { "id": 2, "time": "2018-12-12", "something": 1024 }, { "id": 3, "time": "2018-12-09", "something": 7 }, { "id": 12, "time": "2018-11-12", "something": 7 }, { "id": 13, "time": "2018-11-08", "something": 7 }]'; $items = json_decode($json, true); $expectedArray = []; foreach ($items as $item) { $indexKey = substr($item['time'], 0, 7); if (!isset($expectedArray[$indexKey]) || $expectedArray[$indexKey]['time'] < $item['time']) $expectedArray[$indexKey] = $item; } print_r(json_encode($expectedArray));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 25
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 25
Branch analysis from position: 9
2 jumps found. (Code = 47) Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 24
Branch analysis from position: 21
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/ospfm
function name:  (null)
number of ops:  33
compiled vars:  !0 = $json, !1 = $items, !2 = $expectedArray, !3 = $item, !4 = $indexKey
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                       !0, '%5B%7B%0A%22id%22%3A+1%2C%0A%22time%22%3A+%222018-12-16%22%2C%0A%22pages_indexed%22%3A+1024%0A%7D%2C%0A%7B%0A%22id%22%3A+2%2C%0A%22time%22%3A+%222018-12-12%22%2C%0A%22something%22%3A+1024%0A%7D%2C%0A%7B%0A%22id%22%3A+3%2C%0A%22time%22%3A+%222018-12-09%22%2C%0A%22something%22%3A+7%0A%7D%2C%0A%7B%0A%22id%22%3A+12%2C%0A%22time%22%3A+%222018-11-12%22%2C%0A%22something%22%3A+7%0A%7D%2C%0A%7B%0A%22id%22%3A+13%2C%0A%22time%22%3A+%222018-11-08%22%2C%0A%22something%22%3A+7%0A%7D%5D'
   27     1        INIT_FCALL                                                   'json_decode'
          2        SEND_VAR                                                     !0
          3        SEND_VAL                                                     <true>
          4        DO_ICALL                                             $6      
          5        ASSIGN                                                       !1, $6
   28     6        ASSIGN                                                       !2, <array>
   29     7      > FE_RESET_R                                           $9      !1, ->25
          8    > > FE_FETCH_R                                                   $9, !3, ->25
   30     9    >   FETCH_DIM_R                                          ~10     !3, 'time'
         10        FRAMELESS_ICALL_3                substr              ~11     ~10, 0
         11        OP_DATA                                                      7
         12        ASSIGN                                                       !4, ~11
   31    13        ISSET_ISEMPTY_DIM_OBJ                             0  ~13     !2, !4
         14        BOOL_NOT                                             ~14     ~13
         15      > JMPNZ_EX                                             ~14     ~14, ->21
         16    >   FETCH_DIM_R                                          ~15     !2, !4
         17        FETCH_DIM_R                                          ~16     ~15, 'time'
         18        FETCH_DIM_R                                          ~17     !3, 'time'
         19        IS_SMALLER                                           ~18     ~16, ~17
         20        BOOL                                                 ~14     ~18
         21    > > JMPZ                                                         ~14, ->24
   32    22    >   ASSIGN_DIM                                                   !2, !4
         23        OP_DATA                                                      !3
   29    24    > > JMP                                                          ->8
         25    >   FE_FREE                                                      $9
   34    26        INIT_FCALL                                                   'print_r'
         27        INIT_FCALL                                                   'json_encode'
         28        SEND_VAR                                                     !2
         29        DO_ICALL                                             $20     
         30        SEND_VAR                                                     $20
         31        DO_ICALL                                                     
         32      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
159.41 ms | 2056 KiB | 16 Q