3v4l.org

run code in 300+ 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 = 28
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 28
Branch analysis from position: 9
2 jumps found. (Code = 47) Position 1 = 19, Position 2 = 24
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 27
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 27
Branch analysis from position: 24
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/ospfm
function name:  (null)
number of ops:  36
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, ->28
          8    > > FE_FETCH_R                                               $9, !3, ->28
   30     9    >   INIT_FCALL                                               'substr'
         10        FETCH_DIM_R                                      ~10     !3, 'time'
         11        SEND_VAL                                                 ~10
         12        SEND_VAL                                                 0
         13        SEND_VAL                                                 7
         14        DO_ICALL                                         $11     
         15        ASSIGN                                                   !4, $11
   31    16        ISSET_ISEMPTY_DIM_OBJ                         0  ~13     !2, !4
         17        BOOL_NOT                                         ~14     ~13
         18      > JMPNZ_EX                                         ~14     ~14, ->24
         19    >   FETCH_DIM_R                                      ~15     !2, !4
         20        FETCH_DIM_R                                      ~16     ~15, 'time'
         21        FETCH_DIM_R                                      ~17     !3, 'time'
         22        IS_SMALLER                                       ~18     ~16, ~17
         23        BOOL                                             ~14     ~18
         24    > > JMPZ                                                     ~14, ->27
   32    25    >   ASSIGN_DIM                                               !2, !4
         26        OP_DATA                                                  !3
   29    27    > > JMP                                                      ->8
         28    >   FE_FREE                                                  $9
   34    29        INIT_FCALL                                               'print_r'
         30        INIT_FCALL                                               'json_encode'
         31        SEND_VAR                                                 !2
         32        DO_ICALL                                         $20     
         33        SEND_VAR                                                 $20
         34        DO_ICALL                                                 
         35      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
131.1 ms | 1405 KiB | 21 Q