3v4l.org

run code in 500+ PHP versions simultaneously
<?php $array = [ [ "id" => '6230061c0e88d709ca0d7bbc', 'name' => 'Mobile SamSung', 'slug' => 'mobile-samsung', 'createdAt' => '1648006346' ], [ "id" => '5d1eff529a426778d4b92383', 'name' => 'Mobile Iphone', 'slug' => 'mobile-iphone', 'createdAt' => '1647314181' ], [ "id" => '5d1eff6b9a426778d4b92dc4', 'name' => 'Mobile SamSung', 'slug' => 'mobile-samsung', 'createdAt' => '1647314460' ], [ "id" => '5f894011266aea580b028cb0', 'name' => 'Mobile LG', 'slug' => 'mobile-lg', 'createdAt' => '1647314456' ] ]; $newArray = []; foreach ($array as $key => $value) { $findIndex = array_search($value['slug'], array_column($newArray, 'slug')); if ($findIndex === false) { $newArray[] = $value; } elseif ($findIndex !== false && $newArray[$findIndex]['createdAt'] <= $value['createdAt']) { $newArray[$findIndex] = $value; } } print_r($newArray);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 31
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 31
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 20
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 20
2 jumps found. (Code = 46) Position 1 = 22, Position 2 = 27
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 30
Branch analysis from position: 27
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/f4kRM
function name:  (null)
number of ops:  36
compiled vars:  !0 = $array, !1 = $newArray, !2 = $value, !3 = $key, !4 = $findIndex
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   33     1        ASSIGN                                                       !1, <array>
   34     2      > FE_RESET_R                                           $7      !0, ->31
          3    > > FE_FETCH_R                                           ~8      $7, !2, ->31
          4    >   ASSIGN                                                       !3, ~8
   35     5        INIT_FCALL                                                   'array_search'
          6        FETCH_DIM_R                                          ~10     !2, 'slug'
          7        SEND_VAL                                                     ~10
          8        INIT_FCALL                                                   'array_column'
          9        SEND_VAR                                                     !1
         10        SEND_VAL                                                     'slug'
         11        DO_ICALL                                             $11     
         12        SEND_VAR                                                     $11
         13        DO_ICALL                                             $12     
         14        ASSIGN                                                       !4, $12
   36    15        TYPE_CHECK                                        4          !4
         16      > JMPZ                                                         ~14, ->20
   37    17    >   ASSIGN_DIM                                                   !1
         18        OP_DATA                                                      !2
   36    19      > JMP                                                          ->30
   38    20    >   TYPE_CHECK                                      1018  ~16     !4
         21      > JMPZ_EX                                              ~16     ~16, ->27
         22    >   FETCH_DIM_R                                          ~17     !1, !4
         23        FETCH_DIM_R                                          ~18     ~17, 'createdAt'
         24        FETCH_DIM_R                                          ~19     !2, 'createdAt'
         25        IS_SMALLER_OR_EQUAL                                  ~20     ~18, ~19
         26        BOOL                                                 ~16     ~20
         27    > > JMPZ                                                         ~16, ->30
   39    28    >   ASSIGN_DIM                                                   !1, !4
         29        OP_DATA                                                      !2
   34    30    > > JMP                                                          ->3
         31    >   FE_FREE                                                      $7
   43    32        INIT_FCALL                                                   'print_r'
         33        SEND_VAR                                                     !1
         34        DO_ICALL                                                     
         35      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
157.69 ms | 1991 KiB | 16 Q