3v4l.org

run code in 300+ PHP versions simultaneously
<?php function filterTagsByTypes(array $tags, array $types, $count = 1) { $returnTags = []; sort($types); //Sort from low to high => high priority to low priority usort($tags, function($a, $b) { if ($a->type == $b->type) { return 0; } return $a->type > $b->type ? 1 : -1; }); //Sort tags array from low type to high type. foreach ($types as $type) { foreach ($tags as $tag) { //In this case, since both arrays are sorted, the priority //you are looking for will not arrive. if ($tag->type > $type) { break; //So break out and continue to the next type. } if ($tag->type == $type) { $returnTags[] = $tag; } if (count($returnTags) >= $count) { //End it now! return $returnTags; } } } } // Usage: class Tag { private $type; function __construct($type) { $this->type = $type; } } $tags = [ new Tag(50), new Tag(1), new Tag(25) ]; $types = [1, 50]; var_dump(filterTagsByTypes($tags, $types));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/99BBI
function name:  (null)
number of ops:  22
compiled vars:  !0 = $tags, !1 = $types
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   NEW                                              $2      'Tag'
          1        SEND_VAL_EX                                              50
          2        DO_FCALL                                      0          
          3        INIT_ARRAY                                       ~4      $2
   40     4        NEW                                              $5      'Tag'
          5        SEND_VAL_EX                                              1
          6        DO_FCALL                                      0          
          7        ADD_ARRAY_ELEMENT                                ~4      $5
   41     8        NEW                                              $7      'Tag'
          9        SEND_VAL_EX                                              25
         10        DO_FCALL                                      0          
         11        ADD_ARRAY_ELEMENT                                ~4      $7
   38    12        ASSIGN                                                   !0, ~4
   44    13        ASSIGN                                                   !1, <array>
   46    14        INIT_FCALL                                               'var_dump'
         15        INIT_FCALL                                               'filtertagsbytypes'
         16        SEND_VAR                                                 !0
         17        SEND_VAR                                                 !1
         18        DO_FCALL                                      0  $11     
         19        SEND_VAR                                                 $11
         20        DO_ICALL                                                 
         21      > RETURN                                                   1

Function filtertagsbytypes:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 34
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 34
Branch analysis from position: 14
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 32
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 32
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 20
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 25
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 31
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 25
Branch analysis from position: 32
Branch analysis from position: 32
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
filename:       /in/99BBI
function name:  filterTagsByTypes
number of ops:  36
compiled vars:  !0 = $tags, !1 = $types, !2 = $count, !3 = $returnTags, !4 = $type, !5 = $tag
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      1
    4     3        ASSIGN                                                   !3, <array>
    5     4        INIT_FCALL                                               'sort'
          5        SEND_REF                                                 !1
          6        DO_ICALL                                                 
    6     7        INIT_FCALL                                               'usort'
          8        SEND_REF                                                 !0
          9        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F99BBI%3A6%240'
   11    10        SEND_VAL                                                 ~8
         11        DO_ICALL                                                 
   12    12      > FE_RESET_R                                       $10     !1, ->34
         13    > > FE_FETCH_R                                               $10, !4, ->34
   13    14    > > FE_RESET_R                                       $11     !0, ->32
         15    > > FE_FETCH_R                                               $11, !5, ->32
   16    16    >   FETCH_OBJ_R                                      ~12     !5, 'type'
         17        IS_SMALLER                                               !4, ~12
         18      > JMPZ                                                     ~13, ->20
   17    19    > > JMP                                                      ->32
   19    20    >   FETCH_OBJ_R                                      ~14     !5, 'type'
         21        IS_EQUAL                                                 !4, ~14
         22      > JMPZ                                                     ~15, ->25
   20    23    >   ASSIGN_DIM                                               !3
         24        OP_DATA                                                  !5
   22    25    >   COUNT                                            ~17     !3
         26        IS_SMALLER_OR_EQUAL                                      !2, ~17
         27      > JMPZ                                                     ~18, ->31
   23    28    >   FE_FREE                                                  $11
         29        FE_FREE                                                  $10
         30      > RETURN                                                   !3
   13    31    > > JMP                                                      ->15
         32    >   FE_FREE                                                  $11
   12    33      > JMP                                                      ->13
         34    >   FE_FREE                                                  $10
   27    35      > RETURN                                                   null

End of function filtertagsbytypes

Function %00%7Bclosure%7D%2Fin%2F99BBI%3A6%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/99BBI
function name:  {closure}
number of ops:  16
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    7     2        FETCH_OBJ_R                                      ~2      !0, 'type'
          3        FETCH_OBJ_R                                      ~3      !1, 'type'
          4        IS_EQUAL                                                 ~2, ~3
          5      > JMPZ                                                     ~4, ->7
    8     6    > > RETURN                                                   0
   10     7    >   FETCH_OBJ_R                                      ~5      !0, 'type'
          8        FETCH_OBJ_R                                      ~6      !1, 'type'
          9        IS_SMALLER                                               ~6, ~5
         10      > JMPZ                                                     ~7, ->13
         11    >   QM_ASSIGN                                        ~8      1
         12      > JMP                                                      ->14
         13    >   QM_ASSIGN                                        ~8      -1
         14    > > RETURN                                                   ~8
   11    15*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F99BBI%3A6%240

Class Tag:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/99BBI
function name:  __construct
number of ops:  4
compiled vars:  !0 = $type
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   34     1        ASSIGN_OBJ                                               'type'
          2        OP_DATA                                                  !0
   35     3      > RETURN                                                   null

End of function __construct

End of class Tag.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.87 ms | 1407 KiB | 20 Q