3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Extract values from an array, and return the results * * @param array $array The array to filter * @param callable|string $callback Function to filter the array with * * @return array A new array containing the filtered elements */ function array_excise(array &$array, callable|string $callback): array { $result = []; if (!is_callable($callback)) { return $result; } foreach ($array as $key => $value) { if ($callback($value)) { $result[$key] = $value; unset($array[$key]); } } return $result; } $people = [ ["name" => "Billy Jean", "filter" => false], ["name" => "Ronald Reagan", "filter" => true], ["name" => "Bill Clinton", "filter" => true], ["name" => "Michael Jackson", "filter" => false], ["name" => "Johnny Cash", "filter" => false], ]; $presidents = array_excise($people, function($p){ return $p["filter"]; }); var_export(compact(['people', 'presidents'])); echo "\n---\n"; $numbers = [ [1, 1, 1], [1, 1, 0], [1, 0, 0], [0, 0, 0] ]; $noZeros = array_excise($numbers, 'array_product'); var_export(compact(['numbers', 'noZeros']));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oRu1r
function name:  (null)
number of ops:  27
compiled vars:  !0 = $people, !1 = $presidents, !2 = $numbers, !3 = $noZeros
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   ASSIGN                                                   !0, <array>
   35     1        INIT_FCALL                                               'array_excise'
          2        SEND_REF                                                 !0
          3        DECLARE_LAMBDA_FUNCTION                          ~5      [0]
          4        SEND_VAL                                                 ~5
          5        DO_FCALL                                      0  $6      
          6        ASSIGN                                                   !1, $6
   36     7        INIT_FCALL                                               'var_export'
          8        INIT_FCALL                                               'compact'
          9        SEND_VAL                                                 <array>
         10        DO_ICALL                                         $8      
         11        SEND_VAR                                                 $8
         12        DO_ICALL                                                 
   38    13        ECHO                                                     '%0A---%0A'
   40    14        ASSIGN                                                   !2, <array>
   46    15        INIT_FCALL                                               'array_excise'
         16        SEND_REF                                                 !2
         17        SEND_VAL                                                 'array_product'
         18        DO_FCALL                                      0  $11     
         19        ASSIGN                                                   !3, $11
   47    20        INIT_FCALL                                               'var_export'
         21        INIT_FCALL                                               'compact'
         22        SEND_VAL                                                 <array>
         23        DO_ICALL                                         $13     
         24        SEND_VAR                                                 $13
         25        DO_ICALL                                                 
         26      > 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/oRu1r
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $p
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        FETCH_DIM_R                                      ~1      !0, 'filter'
          2      > RETURN                                                   ~1
          3*     > RETURN                                                   null

End of Dynamic Function 0

Function array_excise:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 21
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 21
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 20
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 20
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/oRu1r
function name:  array_excise
number of ops:  26
compiled vars:  !0 = $array, !1 = $callback, !2 = $result, !3 = $value, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   12     2        ASSIGN                                                   !2, <array>
   13     3        INIT_FCALL                                               'is_callable'
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $6      
          6        BOOL_NOT                                         ~7      $6
          7      > JMPZ                                                     ~7, ->10
   14     8    >   VERIFY_RETURN_TYPE                                       !2
          9      > RETURN                                                   !2
   16    10    > > FE_RESET_R                                       $8      !0, ->21
         11    > > FE_FETCH_R                                       ~9      $8, !3, ->21
         12    >   ASSIGN                                                   !4, ~9
   17    13        INIT_DYNAMIC_CALL                                        !1
         14        SEND_VAR_EX                                              !3
         15        DO_FCALL                                      0  $11     
         16      > JMPZ                                                     $11, ->20
   18    17    >   ASSIGN_DIM                                               !2, !4
         18        OP_DATA                                                  !3
   19    19        UNSET_DIM                                                !0, !4
   16    20    > > JMP                                                      ->11
         21    >   FE_FREE                                                  $8
   23    22        VERIFY_RETURN_TYPE                                       !2
         23      > RETURN                                                   !2
   24    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function array_excise

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
137.85 ms | 1007 KiB | 18 Q