3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Test for array_column // available since PHP >= 5.5.0 !! // php.net/manual/function.array-column.php /** * This file is part of the array_column library * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * * @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com> * @license http://opensource.org/licenses/MIT MIT */ if (!function_exists('array_column')) { /** * Returns the values from a single column of the input array, identified by * the $columnKey. * * Optionally, you may provide an $indexKey to index the values in the returned * array by the values from the $indexKey column in the input array. * * @param array $input A multi-dimensional array (record set) from which to pull * a column of values. * @param mixed $columnKey The column of values to return. This value may be the * integer key of the column you wish to retrieve, or it * may be the string key name for an associative array. * @param mixed $indexKey (Optional.) The column to use as the index/keys for * the returned array. This value may be the integer key * of the column, or it may be the string key name. * @return array */ function array_column($input = null, $columnKey = null, $indexKey = null) { // Using func_get_args() in order to check for proper number of // parameters and trigger errors exactly as the built-in array_column() // does in PHP 5.5. $argc = func_num_args(); $params = func_get_args(); if ($argc < 2) { trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING); return null; } if (!is_array($params[0])) { trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING); return null; } if (!is_int($params[1]) && !is_float($params[1]) && !is_string($params[1]) && $params[1] !== null && !(is_object($params[1]) && method_exists($params[1], '__toString')) ) { trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING); return false; } if (isset($params[2]) && !is_int($params[2]) && !is_float($params[2]) && !is_string($params[2]) && !(is_object($params[2]) && method_exists($params[2], '__toString')) ) { trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING); return false; } $paramsInput = $params[0]; $paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null; $paramsIndexKey = null; if (isset($params[2])) { if (is_float($params[2]) || is_int($params[2])) { $paramsIndexKey = (int) $params[2]; } else { $paramsIndexKey = (string) $params[2]; } } $resultArray = array(); foreach ($paramsInput as $row) { $key = $value = null; $keySet = $valueSet = false; if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) { $keySet = true; $key = (string) $row[$paramsIndexKey]; } if ($paramsColumnKey === null) { $valueSet = true; $value = $row; } elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) { $valueSet = true; $value = $row[$paramsColumnKey]; } if ($valueSet) { if ($keySet) { $resultArray[$key] = $value; } else { $resultArray[] = $value; } } } return $resultArray; } } $foo = ARRAY(); $foo[0] = ARRAY('key'=>'key0', 'value'=>'value0', 'text'=>'text0', 'int'=>1000); $foo[1] = ARRAY('key'=>'key1', 'value'=>'value1', 'text'=>'text1', 'int'=>1001); $foo[2] = ARRAY('key'=>'key2', 'value'=>'value2', 'text'=>'text2', 'int'=>1002); $foo[3] = ARRAY('key'=>'key3', 'value'=>'value3', 'text'=>'text3', 'int'=>1003); $foo[5] = ARRAY('key'=>'key5', 'value'=>'value5', 'text'=>'text5', 'int'=>1005); $foo[7] = ARRAY('key'=>'key7', 'value'=>'value7', 'text'=>'text7', 'int'=>1007); // reset indexes, set column 'value' as values $result1 = array_column($foo, 'value'); var_export($result1); print "\n\n"; // re-index with column 'key', use column 'value' as values $result2 = array_column($foo, 'value', 'key'); var_export($result2); print "\n\n"; // re-index with column 'int', use column 'value' as values $result3 = array_column($foo, 'value', 'int'); var_export($result3); print "\n\n"; // re-index with column 'int', use COMPLETE ARRAY as values $result4 = array_column($foo, NULL, 'int'); var_export($result4); print "\n\n"; // => Warning: array_column() expects at least 2 parameters, 1 given ... $result101 = array_column(NULL); var_export($result101); print "\n\n"; // => Warning: array_column() expects at least 2 parameters, 1 given ... $result102 = array_column(FALSE); var_export($result102); print "\n\n"; // => Warning: array_column() expects at least 2 parameters, 1 given ... $result103 = array_column('bla'); var_export($result103); print "\n\n"; ?>
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/p2SrD
function name:  (null)
number of ops:  83
compiled vars:  !0 = $foo, !1 = $result1, !2 = $result2, !3 = $result3, !4 = $result4, !5 = $result101, !6 = $result102, !7 = $result103
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   INIT_FCALL                                               'function_exists'
          1        SEND_VAL                                                 'array_column'
          2        DO_ICALL                                         $8      
          3        BOOL_NOT                                         ~9      $8
          4      > JMPZ                                                     ~9, ->6
   40     5    >   DECLARE_FUNCTION                                         'array_column'
  129     6    >   ASSIGN                                                   !0, <array>
  130     7        ASSIGN_DIM                                               !0, 0
          8        OP_DATA                                                  <array>
  131     9        ASSIGN_DIM                                               !0, 1
         10        OP_DATA                                                  <array>
  132    11        ASSIGN_DIM                                               !0, 2
         12        OP_DATA                                                  <array>
  133    13        ASSIGN_DIM                                               !0, 3
         14        OP_DATA                                                  <array>
  134    15        ASSIGN_DIM                                               !0, 5
         16        OP_DATA                                                  <array>
  135    17        ASSIGN_DIM                                               !0, 7
         18        OP_DATA                                                  <array>
  138    19        INIT_FCALL                                               'array_column'
         20        SEND_VAR                                                 !0
         21        SEND_VAL                                                 'value'
         22        DO_ICALL                                         $17     
         23        ASSIGN                                                   !1, $17
  139    24        INIT_FCALL                                               'var_export'
         25        SEND_VAR                                                 !1
         26        DO_ICALL                                                 
         27        ECHO                                                     '%0A%0A'
  142    28        INIT_FCALL                                               'array_column'
         29        SEND_VAR                                                 !0
         30        SEND_VAL                                                 'value'
         31        SEND_VAL                                                 'key'
         32        DO_ICALL                                         $20     
         33        ASSIGN                                                   !2, $20
  143    34        INIT_FCALL                                               'var_export'
         35        SEND_VAR                                                 !2
         36        DO_ICALL                                                 
         37        ECHO                                                     '%0A%0A'
  146    38        INIT_FCALL                                               'array_column'
         39        SEND_VAR                                                 !0
         40        SEND_VAL                                                 'value'
         41        SEND_VAL                                                 'int'
         42        DO_ICALL                                         $23     
         43        ASSIGN                                                   !3, $23
  147    44        INIT_FCALL                                               'var_export'
         45        SEND_VAR                                                 !3
         46        DO_ICALL                                                 
         47        ECHO                                                     '%0A%0A'
  150    48        INIT_FCALL                                               'array_column'
         49        SEND_VAR                                                 !0
         50        SEND_VAL                                                 null
         51        SEND_VAL                                                 'int'
         52        DO_ICALL                                         $26     
         53        ASSIGN                                                   !4, $26
  151    54        INIT_FCALL                                               'var_export'
         55        SEND_VAR                                                 !4
         56        DO_ICALL                                                 
         57        ECHO                                                     '%0A%0A'
  156    58        INIT_FCALL                                               'array_column'
         59        SEND_VAL                                                 null
         60        DO_ICALL                                         $29     
         61        ASSIGN                                                   !5, $29
  157    62        INIT_FCALL                                               'var_export'
         63        SEND_VAR                                                 !5
         64        DO_ICALL                                                 
         65        ECHO                                                     '%0A%0A'
  160    66        INIT_FCALL                                               'array_column'
         67        SEND_VAL                                                 <false>
         68        DO_ICALL                                         $32     
         69        ASSIGN                                                   !6, $32
  161    70        INIT_FCALL                                               'var_export'
         71        SEND_VAR                                                 !6
         72        DO_ICALL                                                 
         73        ECHO                                                     '%0A%0A'
  164    74        INIT_FCALL                                               'array_column'
         75        SEND_VAL                                                 'bla'
         76        DO_ICALL                                         $35     
         77        ASSIGN                                                   !7, $35
  165    78        INIT_FCALL                                               'var_export'
         79        SEND_VAR                                                 !7
         80        DO_ICALL                                                 
         81        ECHO                                                     '%0A%0A'
  169    82      > RETURN                                                   1

Function %00array_column%2Fin%2Fp2SrD%3A40%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 17
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 30
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
2 jumps found. (Code = 46) Position 1 = 34, Position 2 = 38
Branch analysis from position: 34
2 jumps found. (Code = 46) Position 1 = 39, Position 2 = 43
Branch analysis from position: 39
2 jumps found. (Code = 46) Position 1 = 44, Position 2 = 47
Branch analysis from position: 44
2 jumps found. (Code = 46) Position 1 = 48, Position 2 = 59
Branch analysis from position: 48
2 jumps found. (Code = 46) Position 1 = 51, Position 2 = 57
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 65
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 65
2 jumps found. (Code = 46) Position 1 = 67, Position 2 = 71
Branch analysis from position: 67
2 jumps found. (Code = 46) Position 1 = 72, Position 2 = 76
Branch analysis from position: 72
2 jumps found. (Code = 46) Position 1 = 77, Position 2 = 81
Branch analysis from position: 77
2 jumps found. (Code = 46) Position 1 = 82, Position 2 = 93
Branch analysis from position: 82
2 jumps found. (Code = 46) Position 1 = 85, Position 2 = 91
Branch analysis from position: 85
2 jumps found. (Code = 43) Position 1 = 94, Position 2 = 99
Branch analysis from position: 94
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 99
2 jumps found. (Code = 43) Position 1 = 104, Position 2 = 108
Branch analysis from position: 104
1 jumps found. (Code = 42) Position 1 = 109
Branch analysis from position: 109
2 jumps found. (Code = 43) Position 1 = 113, Position 2 = 127
Branch analysis from position: 113
2 jumps found. (Code = 47) Position 1 = 116, Position 2 = 119
Branch analysis from position: 116
2 jumps found. (Code = 43) Position 1 = 120, Position 2 = 124
Branch analysis from position: 120
1 jumps found. (Code = 42) Position 1 = 127
Branch analysis from position: 127
2 jumps found. (Code = 77) Position 1 = 129, Position 2 = 164
Branch analysis from position: 129
2 jumps found. (Code = 78) Position 1 = 130, Position 2 = 164
Branch analysis from position: 130
2 jumps found. (Code = 46) Position 1 = 136, Position 2 = 138
Branch analysis from position: 136
2 jumps found. (Code = 43) Position 1 = 139, Position 2 = 143
Branch analysis from position: 139
2 jumps found. (Code = 43) Position 1 = 145, Position 2 = 148
Branch analysis from position: 145
1 jumps found. (Code = 42) Position 1 = 156
Branch analysis from position: 156
2 jumps found. (Code = 43) Position 1 = 157, Position 2 = 163
Branch analysis from position: 157
2 jumps found. (Code = 43) Position 1 = 158, Position 2 = 161
Branch analysis from position: 158
1 jumps found. (Code = 42) Position 1 = 163
Branch analysis from position: 163
1 jumps found. (Code = 42) Position 1 = 129
Branch analysis from position: 129
Branch analysis from position: 161
1 jumps found. (Code = 42) Position 1 = 129
Branch analysis from position: 129
Branch analysis from position: 163
Branch analysis from position: 148
2 jumps found. (Code = 46) Position 1 = 150, Position 2 = 152
Branch analysis from position: 150
2 jumps found. (Code = 43) Position 1 = 153, Position 2 = 156
Branch analysis from position: 153
2 jumps found. (Code = 43) Position 1 = 157, Position 2 = 163
Branch analysis from position: 157
Branch analysis from position: 163
Branch analysis from position: 156
Branch analysis from position: 152
Branch analysis from position: 143
Branch analysis from position: 138
Branch analysis from position: 164
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 164
Branch analysis from position: 124
2 jumps found. (Code = 77) Position 1 = 129, Position 2 = 164
Branch analysis from position: 129
Branch analysis from position: 164
Branch analysis from position: 119
Branch analysis from position: 127
Branch analysis from position: 108
2 jumps found. (Code = 43) Position 1 = 113, Position 2 = 127
Branch analysis from position: 113
Branch analysis from position: 127
Branch analysis from position: 91
Branch analysis from position: 93
Branch analysis from position: 81
Branch analysis from position: 76
Branch analysis from position: 71
Branch analysis from position: 57
Branch analysis from position: 59
Branch analysis from position: 47
Branch analysis from position: 43
Branch analysis from position: 38
filename:       /in/p2SrD
function name:  array_column
number of ops:  167
compiled vars:  !0 = $input, !1 = $columnKey, !2 = $indexKey, !3 = $argc, !4 = $params, !5 = $paramsInput, !6 = $paramsColumnKey, !7 = $paramsIndexKey, !8 = $resultArray, !9 = $row, !10 = $key, !11 = $value, !12 = $keySet, !13 = $valueSet
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV_INIT                                        !0      null
          1        RECV_INIT                                        !1      null
          2        RECV_INIT                                        !2      null
   45     3        FUNC_NUM_ARGS                                    ~14     
          4        ASSIGN                                                   !3, ~14
   46     5        FUNC_GET_ARGS                                    ~16     
          6        ASSIGN                                                   !4, ~16
   48     7        IS_SMALLER                                               !3, 2
          8      > JMPZ                                                     ~18, ->17
   49     9    >   INIT_FCALL                                               'trigger_error'
         10        ROPE_INIT                                     3  ~20     'array_column%28%29+expects+at+least+2+parameters%2C+'
         11        ROPE_ADD                                      1  ~20     ~20, !3
         12        ROPE_END                                      2  ~19     ~20, '+given'
         13        SEND_VAL                                                 ~19
         14        SEND_VAL                                                 512
         15        DO_ICALL                                                 
   50    16      > RETURN                                                   null
   53    17    >   FETCH_DIM_R                                      ~23     !4, 0
         18        TYPE_CHECK                                  128  ~24     ~23
         19        BOOL_NOT                                         ~25     ~24
         20      > JMPZ                                                     ~25, ->30
   54    21    >   INIT_FCALL                                               'trigger_error'
         22        FETCH_DIM_R                                      ~26     !4, 0
         23        GET_TYPE                                         ~27     ~26
         24        CONCAT                                           ~28     'array_column%28%29+expects+parameter+1+to+be+array%2C+', ~27
         25        CONCAT                                           ~29     ~28, '+given'
         26        SEND_VAL                                                 ~29
         27        SEND_VAL                                                 512
         28        DO_ICALL                                                 
   55    29      > RETURN                                                   null
   58    30    >   FETCH_DIM_R                                      ~31     !4, 1
         31        TYPE_CHECK                                   16  ~32     ~31
         32        BOOL_NOT                                         ~33     ~32
         33      > JMPZ_EX                                          ~33     ~33, ->38
   59    34    >   FETCH_DIM_R                                      ~34     !4, 1
         35        TYPE_CHECK                                   32  ~35     ~34
         36        BOOL_NOT                                         ~36     ~35
         37        BOOL                                             ~33     ~36
         38    > > JMPZ_EX                                          ~33     ~33, ->43
   60    39    >   FETCH_DIM_R                                      ~37     !4, 1
         40        TYPE_CHECK                                   64  ~38     ~37
         41        BOOL_NOT                                         ~39     ~38
         42        BOOL                                             ~33     ~39
         43    > > JMPZ_EX                                          ~33     ~33, ->47
   61    44    >   FETCH_DIM_R                                      ~40     !4, 1
         45        TYPE_CHECK                                  1020  ~41     ~40
         46        BOOL                                             ~33     ~41
         47    > > JMPZ_EX                                          ~33     ~33, ->59
   62    48    >   FETCH_DIM_R                                      ~42     !4, 1
         49        TYPE_CHECK                                  256  ~43     ~42
         50      > JMPZ_EX                                          ~43     ~43, ->57
         51    >   INIT_FCALL                                               'method_exists'
         52        FETCH_DIM_R                                      ~44     !4, 1
         53        SEND_VAL                                                 ~44
         54        SEND_VAL                                                 '__toString'
         55        DO_ICALL                                         $45     
         56        BOOL                                             ~43     $45
         57    >   BOOL_NOT                                         ~46     ~43
         58        BOOL                                             ~33     ~46
         59    > > JMPZ                                                     ~33, ->65
   64    60    >   INIT_FCALL                                               'trigger_error'
         61        SEND_VAL                                                 'array_column%28%29%3A+The+column+key+should+be+either+a+string+or+an+integer'
         62        SEND_VAL                                                 512
         63        DO_ICALL                                                 
   65    64      > RETURN                                                   <false>
   68    65    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~48     !4, 2
         66      > JMPZ_EX                                          ~48     ~48, ->71
   69    67    >   FETCH_DIM_R                                      ~49     !4, 2
         68        TYPE_CHECK                                   16  ~50     ~49
         69        BOOL_NOT                                         ~51     ~50
         70        BOOL                                             ~48     ~51
         71    > > JMPZ_EX                                          ~48     ~48, ->76
   70    72    >   FETCH_DIM_R                                      ~52     !4, 2
         73        TYPE_CHECK                                   32  ~53     ~52
         74        BOOL_NOT                                         ~54     ~53
         75        BOOL                                             ~48     ~54
         76    > > JMPZ_EX                                          ~48     ~48, ->81
   71    77    >   FETCH_DIM_R                                      ~55     !4, 2
         78        TYPE_CHECK                                   64  ~56     ~55
         79        BOOL_NOT                                         ~57     ~56
         80        BOOL                                             ~48     ~57
         81    > > JMPZ_EX                                          ~48     ~48, ->93
   72    82    >   FETCH_DIM_R                                      ~58     !4, 2
         83        TYPE_CHECK                                  256  ~59     ~58
         84      > JMPZ_EX                                          ~59     ~59, ->91
         85    >   INIT_FCALL                                               'method_exists'
         86        FETCH_DIM_R                                      ~60     !4, 2
         87        SEND_VAL                                                 ~60
         88        SEND_VAL                                                 '__toString'
         89        DO_ICALL                                         $61     
         90        BOOL                                             ~59     $61
         91    >   BOOL_NOT                                         ~62     ~59
         92        BOOL                                             ~48     ~62
         93    > > JMPZ                                                     ~48, ->99
   74    94    >   INIT_FCALL                                               'trigger_error'
         95        SEND_VAL                                                 'array_column%28%29%3A+The+index+key+should+be+either+a+string+or+an+integer'
         96        SEND_VAL                                                 512
         97        DO_ICALL                                                 
   75    98      > RETURN                                                   <false>
   78    99    >   FETCH_DIM_R                                      ~64     !4, 0
        100        ASSIGN                                                   !5, ~64
   79   101        FETCH_DIM_R                                      ~66     !4, 1
        102        TYPE_CHECK                                  1020          ~66
        103      > JMPZ                                                     ~67, ->108
        104    >   FETCH_DIM_R                                      ~68     !4, 1
        105        CAST                                          6  ~69     ~68
        106        QM_ASSIGN                                        ~70     ~69
        107      > JMP                                                      ->109
        108    >   QM_ASSIGN                                        ~70     null
        109    >   ASSIGN                                                   !6, ~70
   81   110        ASSIGN                                                   !7, null
   82   111        ISSET_ISEMPTY_DIM_OBJ                         0          !4, 2
        112      > JMPZ                                                     ~73, ->127
   83   113    >   FETCH_DIM_R                                      ~74     !4, 2
        114        TYPE_CHECK                                   32  ~75     ~74
        115      > JMPNZ_EX                                         ~75     ~75, ->119
        116    >   FETCH_DIM_R                                      ~76     !4, 2
        117        TYPE_CHECK                                   16  ~77     ~76
        118        BOOL                                             ~75     ~77
        119    > > JMPZ                                                     ~75, ->124
   84   120    >   FETCH_DIM_R                                      ~78     !4, 2
        121        CAST                                          4  ~79     ~78
        122        ASSIGN                                                   !7, ~79
        123      > JMP                                                      ->127
   86   124    >   FETCH_DIM_R                                      ~81     !4, 2
        125        CAST                                          6  ~82     ~81
        126        ASSIGN                                                   !7, ~82
   90   127    >   ASSIGN                                                   !8, <array>
   92   128      > FE_RESET_R                                       $85     !5, ->164
        129    > > FE_FETCH_R                                               $85, !9, ->164
   94   130    >   ASSIGN                                           ~86     !11, null
        131        ASSIGN                                                   !10, ~86
   95   132        ASSIGN                                           ~88     !13, <false>
        133        ASSIGN                                                   !12, ~88
   97   134        TYPE_CHECK                                  1020  ~90     !7
        135      > JMPZ_EX                                          ~90     ~90, ->138
        136    >   ARRAY_KEY_EXISTS                                 ~91     !7, !9
        137        BOOL                                             ~90     ~91
        138    > > JMPZ                                                     ~90, ->143
   98   139    >   ASSIGN                                                   !12, <true>
   99   140        FETCH_DIM_R                                      ~93     !9, !7
        141        CAST                                          6  ~94     ~93
        142        ASSIGN                                                   !10, ~94
  102   143    >   TYPE_CHECK                                    2          !6
        144      > JMPZ                                                     ~96, ->148
  103   145    >   ASSIGN                                                   !13, <true>
  104   146        ASSIGN                                                   !11, !9
        147      > JMP                                                      ->156
  105   148    >   TYPE_CHECK                                  128  ~99     !9
        149      > JMPZ_EX                                          ~99     ~99, ->152
        150    >   ARRAY_KEY_EXISTS                                 ~100    !6, !9
        151        BOOL                                             ~99     ~100
        152    > > JMPZ                                                     ~99, ->156
  106   153    >   ASSIGN                                                   !13, <true>
  107   154        FETCH_DIM_R                                      ~102    !9, !6
        155        ASSIGN                                                   !11, ~102
  110   156    > > JMPZ                                                     !13, ->163
  111   157    > > JMPZ                                                     !12, ->161
  112   158    >   ASSIGN_DIM                                               !8, !10
        159        OP_DATA                                                  !11
        160      > JMP                                                      ->163
  114   161    >   ASSIGN_DIM                                               !8
        162        OP_DATA                                                  !11
   92   163    > > JMP                                                      ->129
        164    >   FE_FREE                                                  $85
  120   165      > RETURN                                                   !8
  121   166*     > RETURN                                                   null

End of function %00array_column%2Fin%2Fp2SrD%3A40%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.14 ms | 1412 KiB | 23 Q