3v4l.org

run code in 300+ PHP versions simultaneously
<?php if ( ! function_exists('subarray_element')) { /** * Gets the sub-array containing a value of certain key present in it * * @param array $arr contains arrays with key => val pairs * @param string $id_key key of the subarray to filter * @param string $id_val value of the corresponding key * @return first element matching the value provided or FALSE * @link https://stackoverflow.com/q/13933454/6225838 * @author CPHPython */ function subarray_element($arr, $id_key, $id_val = NULL) { return current(array_filter( $arr, function ($subarr) use($id_key, $id_val) { if(array_key_exists($id_key, $subarr)) return $subarr[$id_key] == $id_val; } )); } } $list = [ [ 'id' => '3243', 'link' => 'fruits', 'lev' => '1', ], [ 'id' => '6546', 'link' => 'apple', 'lev' => '2', ], [ 'id' => '9348', 'link' => 'orange', 'lev' => '2', ], [ 'id' => '9000', 'link' => 'orange', 'lev' => '2', ] ]; echo 'Matching pair: '.var_export(subarray_element($list, 'id', 9348), TRUE).PHP_EOL.'-------'.PHP_EOL. 'Non-matching pair: '.var_export(subarray_element($list, 'id', 42), TRUE).PHP_EOL.'-------'.PHP_EOL. 'Duplicate pair: '.var_export(subarray_element($list, 'link', 'orange'), TRUE).PHP_EOL.'-------'.PHP_EOL; usort($list, function($f1, $f2){ return $f1['id'] > $f2['id']; }); echo 'Duplicate pair after sortind by id: '.var_export(subarray_element($list, 'link', 'orange'), TRUE);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
filename:       /in/XioRa
function name:  (null)
number of ops:  66
compiled vars:  !0 = $list
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'function_exists'
          1        SEND_VAL                                                 'subarray_element'
          2        DO_ICALL                                         $1      
          3        BOOL_NOT                                         ~2      $1
          4      > JMPZ                                                     ~2, ->6
   14     5    >   DECLARE_FUNCTION                                         'subarray_element'
   25     6    >   ASSIGN                                                   !0, <array>
   48     7        INIT_FCALL                                               'var_export'
          8        INIT_FCALL_BY_NAME                                       'subarray_element'
          9        SEND_VAR_EX                                              !0
         10        SEND_VAL_EX                                              'id'
         11        SEND_VAL_EX                                              9348
         12        DO_FCALL                                      0  $4      
         13        SEND_VAR                                                 $4
         14        SEND_VAL                                                 <true>
         15        DO_ICALL                                         $5      
         16        CONCAT                                           ~6      'Matching+pair%3A+', $5
         17        CONCAT                                           ~7      ~6, '%0A'
         18        CONCAT                                           ~8      ~7, '-------'
         19        CONCAT                                           ~9      ~8, '%0A'
   49    20        CONCAT                                           ~10     ~9, 'Non-matching+pair%3A+'
         21        INIT_FCALL                                               'var_export'
         22        INIT_FCALL_BY_NAME                                       'subarray_element'
         23        SEND_VAR_EX                                              !0
         24        SEND_VAL_EX                                              'id'
         25        SEND_VAL_EX                                              42
         26        DO_FCALL                                      0  $11     
         27        SEND_VAR                                                 $11
         28        SEND_VAL                                                 <true>
         29        DO_ICALL                                         $12     
         30        CONCAT                                           ~13     ~10, $12
         31        CONCAT                                           ~14     ~13, '%0A'
         32        CONCAT                                           ~15     ~14, '-------'
         33        CONCAT                                           ~16     ~15, '%0A'
   50    34        CONCAT                                           ~17     ~16, 'Duplicate+pair%3A+'
         35        INIT_FCALL                                               'var_export'
         36        INIT_FCALL_BY_NAME                                       'subarray_element'
         37        SEND_VAR_EX                                              !0
         38        SEND_VAL_EX                                              'link'
         39        SEND_VAL_EX                                              'orange'
         40        DO_FCALL                                      0  $18     
         41        SEND_VAR                                                 $18
         42        SEND_VAL                                                 <true>
         43        DO_ICALL                                         $19     
         44        CONCAT                                           ~20     ~17, $19
         45        CONCAT                                           ~21     ~20, '%0A'
         46        CONCAT                                           ~22     ~21, '-------'
         47        CONCAT                                           ~23     ~22, '%0A'
         48        ECHO                                                     ~23
   51    49        INIT_FCALL                                               'usort'
         50        SEND_REF                                                 !0
         51        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FXioRa%3A51%242'
         52        SEND_VAL                                                 ~24
         53        DO_ICALL                                                 
   52    54        INIT_FCALL                                               'var_export'
         55        INIT_FCALL_BY_NAME                                       'subarray_element'
         56        SEND_VAR_EX                                              !0
         57        SEND_VAL_EX                                              'link'
         58        SEND_VAL_EX                                              'orange'
         59        DO_FCALL                                      0  $26     
         60        SEND_VAR                                                 $26
         61        SEND_VAL                                                 <true>
         62        DO_ICALL                                         $27     
         63        CONCAT                                           ~28     'Duplicate+pair+after+sortind+by+id%3A+', $27
         64        ECHO                                                     ~28
         65      > RETURN                                                   1

Function %00subarray_element%2Fin%2FXioRa%3A14%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XioRa
function name:  subarray_element
number of ops:  15
compiled vars:  !0 = $arr, !1 = $id_key, !2 = $id_val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
   15     3        INIT_FCALL                                               'current'
          4        INIT_FCALL                                               'array_filter'
   16     5        SEND_VAR                                                 !0
   17     6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FXioRa%3A17%241'
          7        BIND_LEXICAL                                             ~3, !1
          8        BIND_LEXICAL                                             ~3, !2
   20     9        SEND_VAL                                                 ~3
         10        DO_ICALL                                         $4      
         11        SEND_VAR                                                 $4
         12        DO_ICALL                                         $5      
         13      > RETURN                                                   $5
   22    14*     > RETURN                                                   null

End of function %00subarray_element%2Fin%2FXioRa%3A14%240

Function %00%7Bclosure%7D%2Fin%2FXioRa%3A17%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XioRa
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $subarr, !1 = $id_key, !2 = $id_val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   18     3        ARRAY_KEY_EXISTS                                         !1, !0
          4      > JMPZ                                                     ~3, ->8
   19     5    >   FETCH_DIM_R                                      ~4      !0, !1
          6        IS_EQUAL                                         ~5      !2, ~4
          7      > RETURN                                                   ~5
   20     8    > > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FXioRa%3A17%241

Function %00%7Bclosure%7D%2Fin%2FXioRa%3A51%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XioRa
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $f1, !1 = $f2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        FETCH_DIM_R                                      ~2      !0, 'id'
          3        FETCH_DIM_R                                      ~3      !1, 'id'
          4        IS_SMALLER                                       ~4      ~3, ~2
          5      > RETURN                                                   ~4
          6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FXioRa%3A51%242

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
277.64 ms | 1404 KiB | 24 Q