3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '[ { "name": "D\'Amore Ltd", "id": 67, "catId": 1 }, { "name": "D\'Amore Ltd", "id": 67, "catId": 2 }, { "name": "D\'Amore Ltd", "id": 67, "catId": 3 }, { "name": "Kuvalis, Denesik and Terry", "id": 14, "catId": 1 }, { "name": "Labadie-Bauch", "id": 1, "catId": 1 }, { "name": "Labadie-Bauch", "id": 1, "catId": 2 } ]'; $locations = json_decode($json); $categories = [1, 2]; $collection = []; $grouped = array_reduce($locations, function ($grouped, $location) { $grouped[$location->name][] = $location->catId; return $grouped; }, []); $grouped = array_filter($grouped, function ($_categories) use ($categories) { return ! array_diff($categories, $_categories); }); foreach ($locations as $location) { if (isset($grouped[$location->name]) && in_array($location->catId, $categories)) { $collection[] = $location; } } var_dump($collection);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 22, Position 2 = 36
Branch analysis from position: 22
2 jumps found. (Code = 78) Position 1 = 23, Position 2 = 36
Branch analysis from position: 23
2 jumps found. (Code = 46) Position 1 = 26, Position 2 = 32
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 35
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 35
Branch analysis from position: 32
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
filename:       /in/mBZZn
function name:  (null)
number of ops:  41
compiled vars:  !0 = $json, !1 = $locations, !2 = $categories, !3 = $collection, !4 = $grouped, !5 = $location
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%5B%0A++++%7B%0A++++++++%22name%22%3A+%22D%27Amore+Ltd%22%2C%0A++++++++%22id%22%3A+67%2C%0A++++++++%22catId%22%3A+1%0A++++%7D%2C+%7B%0A++++++++%22name%22%3A+%22D%27Amore+Ltd%22%2C%0A++++++++%22id%22%3A+67%2C%0A++++++++%22catId%22%3A+2%0A++++%7D%2C+%7B%0A++++++++%22name%22%3A+%22D%27Amore+Ltd%22%2C%0A++++++++%22id%22%3A+67%2C%0A++++++++%22catId%22%3A+3%0A++++%7D%2C+%7B%0A++++++++%22name%22%3A+%22Kuvalis%2C+Denesik+and+Terry%22%2C%0A++++++++%22id%22%3A+14%2C%0A++++++++%22catId%22%3A+1%0A++++%7D%2C+%7B%0A++++++++%22name%22%3A+%22Labadie-Bauch%22%2C%0A++++++++%22id%22%3A+1%2C%0A++++++++%22catId%22%3A+1%0A++++%7D%2C+%7B%0A++++++++%22name%22%3A+%22Labadie-Bauch%22%2C%0A++++++++%22id%22%3A+1%2C%0A++++++++%22catId%22%3A+2%0A++++%7D%0A%5D'
   31     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $7      
          4        ASSIGN                                                   !1, $7
   32     5        ASSIGN                                                   !2, <array>
   33     6        ASSIGN                                                   !3, <array>
   35     7        INIT_FCALL                                               'array_reduce'
          8        SEND_VAR                                                 !1
          9        DECLARE_LAMBDA_FUNCTION                          ~11     [0]
   38    10        SEND_VAL                                                 ~11
         11        SEND_VAL                                                 <array>
   35    12        DO_ICALL                                         $12     
         13        ASSIGN                                                   !4, $12
   40    14        INIT_FCALL                                               'array_filter'
         15        SEND_VAR                                                 !4
         16        DECLARE_LAMBDA_FUNCTION                          ~14     [1]
         17        BIND_LEXICAL                                             ~14, !2
   42    18        SEND_VAL                                                 ~14
   40    19        DO_ICALL                                         $15     
         20        ASSIGN                                                   !4, $15
   44    21      > FE_RESET_R                                       $17     !1, ->36
         22    > > FE_FETCH_R                                               $17, !5, ->36
   45    23    >   FETCH_OBJ_R                                      ~18     !5, 'name'
         24        ISSET_ISEMPTY_DIM_OBJ                         0  ~19     !4, ~18
         25      > JMPZ_EX                                          ~19     ~19, ->32
         26    >   INIT_FCALL                                               'in_array'
         27        FETCH_OBJ_R                                      ~20     !5, 'catId'
         28        SEND_VAL                                                 ~20
         29        SEND_VAR                                                 !2
         30        DO_ICALL                                         $21     
         31        BOOL                                             ~19     $21
         32    > > JMPZ                                                     ~19, ->35
   46    33    >   ASSIGN_DIM                                               !3
         34        OP_DATA                                                  !5
   44    35    > > JMP                                                      ->22
         36    >   FE_FREE                                                  $17
   51    37        INIT_FCALL                                               'var_dump'
         38        SEND_VAR                                                 !3
         39        DO_ICALL                                                 
         40      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mBZZn
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $grouped, !1 = $location
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   36     2        FETCH_OBJ_R                                      ~2      !1, 'name'
          3        FETCH_OBJ_R                                      ~5      !1, 'catId'
          4        FETCH_DIM_W                                      $3      !0, ~2
          5        ASSIGN_DIM                                               $3
          6        OP_DATA                                                  ~5
   37     7      > RETURN                                                   !0
   38     8*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mBZZn
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $_categories, !1 = $categories
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   41     2        INIT_FCALL                                               'array_diff'
          3        SEND_VAR                                                 !1
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6        BOOL_NOT                                         ~3      $2
          7      > RETURN                                                   ~3
   42     8*     > RETURN                                                   null

End of Dynamic Function 1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.09 ms | 1021 KiB | 19 Q