3v4l.org

run code in 300+ PHP versions simultaneously
<?php function test($array) { try { return is_indexed_array($array); } catch (Exception $e) { echo $e->getMessage(); return 'error'; } } function is_indexed_array($array) { if (!is_array($array)) return false; foreach (array_keys($array) as $k => $v) if ($k !== $v) return false; return true; } // non-array - adjust according to your preferred error handling technique assert(test(null) === false); assert(test(false) === false); assert(test(new stdClass) === false); // empty array assert(test([]) === true); // numeric keys assert(test([null, 'a', 'b']) === true); // proper indexed array; also, to catch wrong isset usage assert(test([1 => 'a', 2 => 'b', 3 => 'c']) === false); // sequential but not 0-based assert(test([-1 => '', 0 => '', 1 => 'a']) === false); // sequential but not 0-based assert(test([0 => '', 2 => 'b', 3 => 'c']) === false); // 1 index missing assert(test([0 => '', 2 => 'b', 1 => 'a']) === false); // numeric but unordered assert(test([0 => '']) === true); // single item assert(test([1 => 'a']) === false); // single item assert(test([NAN]) === true); assert(test([NAN => '']) === false); assert(test([INF]) === true); assert(test([INF => '']) === false); // string keys assert(test(['' => '', 'a' => 'a', 'b' => 'b']) === false); // proper associative array assert(test(['' => '']) === false); // single item; also, associative even though '' == 0 // mixed keys assert(test(['' => '', 1 => 'a']) === false); // first key string but rest not assert(test(['' => '', 0 => '']) === false); // first key string but rest not assert(test([0 => '', 'a' => 'a']) === false); // first key numeric but rest not // numeric string keys assert(test([' 0' => '']) === false); // associative even though ' 0' == 0 assert(test(['00' => '']) === false); // associative even though '00' == 0 assert(test(['0*' => '']) === false); // associative even though '0*' == 0 assert(test(['0.0' => '']) === false); // associative even though '0.0' == 0.0 assert(test(['0x0' => '']) === false); // associative even though '0x0' == 0 assert(test(['0e0' => '']) === false); // associative even though '0e0' == 0 $obj = new stdClass; $obj->{0} = ''; assert(test((array)$obj) === false); // to catch ['0' => ''] like situations
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2hbc3
function name:  (null)
number of ops:  252
compiled vars:  !0 = $obj
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   ASSERT_CHECK                                             
          1        INIT_FCALL                                               'assert'
          2        INIT_FCALL                                               'test'
          3        SEND_VAL                                                 null
          4        DO_FCALL                                      0  $1      
          5        TYPE_CHECK                                    4  ~2      $1
          6        SEND_VAL                                                 ~2
          7        SEND_VAL                                                 'assert%28test%28null%29+%3D%3D%3D+false%29'
          8        DO_ICALL                                                 
   32     9        ASSERT_CHECK                                             
         10        INIT_FCALL                                               'assert'
         11        INIT_FCALL                                               'test'
         12        SEND_VAL                                                 <false>
         13        DO_FCALL                                      0  $4      
         14        TYPE_CHECK                                    4  ~5      $4
         15        SEND_VAL                                                 ~5
         16        SEND_VAL                                                 'assert%28test%28false%29+%3D%3D%3D+false%29'
         17        DO_ICALL                                                 
   33    18        ASSERT_CHECK                                             
         19        INIT_FCALL                                               'assert'
         20        INIT_FCALL                                               'test'
         21        NEW                                              $7      'stdClass'
         22        DO_FCALL                                      0          
         23        SEND_VAR                                                 $7
         24        DO_FCALL                                      0  $9      
         25        TYPE_CHECK                                    4  ~10     $9
         26        SEND_VAL                                                 ~10
         27        SEND_VAL                                                 'assert%28test%28new+stdClass%28%29%29+%3D%3D%3D+false%29'
         28        DO_ICALL                                                 
   36    29        ASSERT_CHECK                                             
         30        INIT_FCALL                                               'assert'
         31        INIT_FCALL                                               'test'
         32        SEND_VAL                                                 <array>
         33        DO_FCALL                                      0  $12     
         34        TYPE_CHECK                                    8  ~13     $12
         35        SEND_VAL                                                 ~13
         36        SEND_VAL                                                 'assert%28test%28%5B%5D%29+%3D%3D%3D+true%29'
         37        DO_ICALL                                                 
   39    38        ASSERT_CHECK                                             
         39        INIT_FCALL                                               'assert'
         40        INIT_FCALL                                               'test'
         41        SEND_VAL                                                 <array>
         42        DO_FCALL                                      0  $15     
         43        TYPE_CHECK                                    8  ~16     $15
         44        SEND_VAL                                                 ~16
         45        SEND_VAL                                                 'assert%28test%28%5Bnull%2C+%27a%27%2C+%27b%27%5D%29+%3D%3D%3D+true%29'
         46        DO_ICALL                                                 
   40    47        ASSERT_CHECK                                             
         48        INIT_FCALL                                               'assert'
         49        INIT_FCALL                                               'test'
         50        SEND_VAL                                                 <array>
         51        DO_FCALL                                      0  $18     
         52        TYPE_CHECK                                    4  ~19     $18
         53        SEND_VAL                                                 ~19
         54        SEND_VAL                                                 'assert%28test%28%5B1+%3D%3E+%27a%27%2C+2+%3D%3E+%27b%27%2C+3+%3D%3E+%27c%27%5D%29+%3D%3D%3D+false%29'
         55        DO_ICALL                                                 
   41    56        ASSERT_CHECK                                             
         57        INIT_FCALL                                               'assert'
         58        INIT_FCALL                                               'test'
         59        SEND_VAL                                                 <array>
         60        DO_FCALL                                      0  $21     
         61        TYPE_CHECK                                    4  ~22     $21
         62        SEND_VAL                                                 ~22
         63        SEND_VAL                                                 'assert%28test%28%5B-1+%3D%3E+%27%27%2C+0+%3D%3E+%27%27%2C+1+%3D%3E+%27a%27%5D%29+%3D%3D%3D+false%29'
         64        DO_ICALL                                                 
   42    65        ASSERT_CHECK                                             
         66        INIT_FCALL                                               'assert'
         67        INIT_FCALL                                               'test'
         68        SEND_VAL                                                 <array>
         69        DO_FCALL                                      0  $24     
         70        TYPE_CHECK                                    4  ~25     $24
         71        SEND_VAL                                                 ~25
         72        SEND_VAL                                                 'assert%28test%28%5B0+%3D%3E+%27%27%2C+2+%3D%3E+%27b%27%2C+3+%3D%3E+%27c%27%5D%29+%3D%3D%3D+false%29'
         73        DO_ICALL                                                 
   43    74        ASSERT_CHECK                                             
         75        INIT_FCALL                                               'assert'
         76        INIT_FCALL                                               'test'
         77        SEND_VAL                                                 <array>
         78        DO_FCALL                                      0  $27     
         79        TYPE_CHECK                                    4  ~28     $27
         80        SEND_VAL                                                 ~28
         81        SEND_VAL                                                 'assert%28test%28%5B0+%3D%3E+%27%27%2C+2+%3D%3E+%27b%27%2C+1+%3D%3E+%27a%27%5D%29+%3D%3D%3D+false%29'
         82        DO_ICALL                                                 
   44    83        ASSERT_CHECK                                             
         84        INIT_FCALL                                               'assert'
         85        INIT_FCALL                                               'test'
         86        SEND_VAL                                                 <array>
         87        DO_FCALL                                      0  $30     
         88        TYPE_CHECK                                    8  ~31     $30
         89        SEND_VAL                                                 ~31
         90        SEND_VAL                                                 'assert%28test%28%5B0+%3D%3E+%27%27%5D%29+%3D%3D%3D+true%29'
         91        DO_ICALL                                                 
   45    92        ASSERT_CHECK                                             
         93        INIT_FCALL                                               'assert'
         94        INIT_FCALL                                               'test'
         95        SEND_VAL                                                 <array>
         96        DO_FCALL                                      0  $33     
         97        TYPE_CHECK                                    4  ~34     $33
         98        SEND_VAL                                                 ~34
         99        SEND_VAL                                                 'assert%28test%28%5B1+%3D%3E+%27a%27%5D%29+%3D%3D%3D+false%29'
        100        DO_ICALL                                                 
   46   101        ASSERT_CHECK                                             
        102        INIT_FCALL                                               'assert'
        103        INIT_FCALL                                               'test'
        104        SEND_VAL                                                 <array>
        105        DO_FCALL                                      0  $36     
        106        TYPE_CHECK                                    8  ~37     $36
        107        SEND_VAL                                                 ~37
        108        SEND_VAL                                                 'assert%28test%28%5BNAN%5D%29+%3D%3D%3D+true%29'
        109        DO_ICALL                                                 
   47   110        ASSERT_CHECK                                             
        111        INIT_FCALL                                               'assert'
        112        INIT_FCALL                                               'test'
        113        SEND_VAL                                                 <array>
        114        DO_FCALL                                      0  $39     
        115        TYPE_CHECK                                    4  ~40     $39
        116        SEND_VAL                                                 ~40
        117        SEND_VAL                                                 'assert%28test%28%5BNAN+%3D%3E+%27%27%5D%29+%3D%3D%3D+false%29'
        118        DO_ICALL                                                 
   48   119        ASSERT_CHECK                                             
        120        INIT_FCALL                                               'assert'
        121        INIT_FCALL                                               'test'
        122        SEND_VAL                                                 <array>
        123        DO_FCALL                                      0  $42     
        124        TYPE_CHECK                                    8  ~43     $42
        125        SEND_VAL                                                 ~43
        126        SEND_VAL                                                 'assert%28test%28%5BINF%5D%29+%3D%3D%3D+true%29'
        127        DO_ICALL                                                 
   49   128        ASSERT_CHECK                                             
        129        INIT_FCALL                                               'assert'
        130        INIT_FCALL                                               'test'
        131        SEND_VAL                                                 <array>
        132        DO_FCALL                                      0  $45     
        133        TYPE_CHECK                                    4  ~46     $45
        134        SEND_VAL                                                 ~46
        135        SEND_VAL                                                 'assert%28test%28%5BINF+%3D%3E+%27%27%5D%29+%3D%3D%3D+false%29'
        136        DO_ICALL                                                 
   52   137        ASSERT_CHECK                                             
        138        INIT_FCALL                                               'assert'
        139        INIT_FCALL                                               'test'
        140        SEND_VAL                                                 <array>
        141        DO_FCALL                                      0  $48     
        142        TYPE_CHECK                                    4  ~49     $48
        143        SEND_VAL                                                 ~49
        144        SEND_VAL                                                 'assert%28test%28%5B%27%27+%3D%3E+%27%27%2C+%27a%27+%3D%3E+%27a%27%2C+%27b%27+%3D%3E+%27b%27%5D%29+%3D%3D%3D+false%29'
        145        DO_ICALL                                                 
   53   146        ASSERT_CHECK                                             
        147        INIT_FCALL                                               'assert'
        148        INIT_FCALL                                               'test'
        149        SEND_VAL                                                 <array>
        150        DO_FCALL                                      0  $51     
        151        TYPE_CHECK                                    4  ~52     $51
        152        SEND_VAL                                                 ~52
        153        SEND_VAL                                                 'assert%28test%28%5B%27%27+%3D%3E+%27%27%5D%29+%3D%3D%3D+false%29'
        154        DO_ICALL                                                 
   56   155        ASSERT_CHECK                                             
        156        INIT_FCALL                                               'assert'
        157        INIT_FCALL                                               'test'
        158        SEND_VAL                                                 <array>
        159        DO_FCALL                                      0  $54     
        160        TYPE_CHECK                                    4  ~55     $54
        161        SEND_VAL                                                 ~55
        162        SEND_VAL                                                 'assert%28test%28%5B%27%27+%3D%3E+%27%27%2C+1+%3D%3E+%27a%27%5D%29+%3D%3D%3D+false%29'
        163        DO_ICALL                                                 
   57   164        ASSERT_CHECK                                             
        165        INIT_FCALL                                               'assert'
        166        INIT_FCALL                                               'test'
        167        SEND_VAL                                                 <array>
        168        DO_FCALL                                      0  $57     
        169        TYPE_CHECK                                    4  ~58     $57
        170        SEND_VAL                                                 ~58
        171        SEND_VAL                                                 'assert%28test%28%5B%27%27+%3D%3E+%27%27%2C+0+%3D%3E+%27%27%5D%29+%3D%3D%3D+false%29'
        172        DO_ICALL                                                 
   58   173        ASSERT_CHECK                                             
        174        INIT_FCALL                                               'assert'
        175        INIT_FCALL                                               'test'
        176        SEND_VAL                                                 <array>
        177        DO_FCALL                                      0  $60     
        178        TYPE_CHECK                                    4  ~61     $60
        179        SEND_VAL                                                 ~61
        180        SEND_VAL                                                 'assert%28test%28%5B0+%3D%3E+%27%27%2C+%27a%27+%3D%3E+%27a%27%5D%29+%3D%3D%3D+false%29'
        181        DO_ICALL                                                 
   61   182        ASSERT_CHECK                                             
        183        INIT_FCALL                                               'assert'
        184        INIT_FCALL                                               'test'
        185        SEND_VAL                                                 <array>
        186        DO_FCALL                                      0  $63     
        187        TYPE_CHECK                                    4  ~64     $63
        188        SEND_VAL                                                 ~64
        189        SEND_VAL                                                 'assert%28test%28%5B%27+0%27+%3D%3E+%27%27%5D%29+%3D%3D%3D+false%29'
        190        DO_ICALL                                                 
   62   191        ASSERT_CHECK                                             
        192        INIT_FCALL                                               'assert'
        193        INIT_FCALL                                               'test'
        194        SEND_VAL                                                 <array>
        195        DO_FCALL                                      0  $66     
        196        TYPE_CHECK                                    4  ~67     $66
        197        SEND_VAL                                                 ~67
        198        SEND_VAL                                                 'assert%28test%28%5B%2700%27+%3D%3E+%27%27%5D%29+%3D%3D%3D+false%29'
        199        DO_ICALL                                                 
   63   200        ASSERT_CHECK                                             
        201        INIT_FCALL                                               'assert'
        202        INIT_FCALL                                               'test'
        203        SEND_VAL                                                 <array>
        204        DO_FCALL                                      0  $69     
        205        TYPE_CHECK                                    4  ~70     $69
        206        SEND_VAL                                                 ~70
        207        SEND_VAL                                                 'assert%28test%28%5B%270%2A%27+%3D%3E+%27%27%5D%29+%3D%3D%3D+false%29'
        208        DO_ICALL                                                 
   64   209        ASSERT_CHECK                                             
        210        INIT_FCALL                                               'assert'
        211        INIT_FCALL                                               'test'
        212        SEND_VAL                                                 <array>
        213        DO_FCALL                                      0  $72     
        214        TYPE_CHECK                                    4  ~73     $72
        215        SEND_VAL                                                 ~73
        216        SEND_VAL                                                 'assert%28test%28%5B%270.0%27+%3D%3E+%27%27%5D%29+%3D%3D%3D+false%29'
        217        DO_ICALL                                                 
   65   218        ASSERT_CHECK                                             
        219        INIT_FCALL                                               'assert'
        220        INIT_FCALL                                               'test'
        221        SEND_VAL                                                 <array>
        222        DO_FCALL                                      0  $75     
        223        TYPE_CHECK                                    4  ~76     $75
        224        SEND_VAL                                                 ~76
        225        SEND_VAL                                                 'assert%28test%28%5B%270x0%27+%3D%3E+%27%27%5D%29+%3D%3D%3D+false%29'
        226        DO_ICALL                                                 
   66   227        ASSERT_CHECK                                             
        228        INIT_FCALL                                               'assert'
        229        INIT_FCALL                                               'test'
        230        SEND_VAL                                                 <array>
        231        DO_FCALL                                      0  $78     
        232        TYPE_CHECK                                    4  ~79     $78
        233        SEND_VAL                                                 ~79
        234        SEND_VAL                                                 'assert%28test%28%5B%270e0%27+%3D%3E+%27%27%5D%29+%3D%3D%3D+false%29'
        235        DO_ICALL                                                 
   67   236        NEW                                              $81     'stdClass'
        237        DO_FCALL                                      0          
        238        ASSIGN                                                   !0, $81
        239        ASSIGN_OBJ                                               !0, '0'
        240        OP_DATA                                                  ''
   68   241        ASSERT_CHECK                                             
        242        INIT_FCALL                                               'assert'
        243        INIT_FCALL                                               'test'
        244        CAST                                          7  ~85     !0
        245        SEND_VAL                                                 ~85
        246        DO_FCALL                                      0  $86     
        247        TYPE_CHECK                                    4  ~87     $86
        248        SEND_VAL                                                 ~87
        249        SEND_VAL                                                 'assert%28test%28%28array%29%24obj%29+%3D%3D%3D+false%29'
        250        DO_ICALL                                                 
        251      > RETURN                                                   1

Function test:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 6
Branch analysis from position: 6
2 jumps found. (Code = 107) Position 1 = 7, Position 2 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2hbc3
function name:  test
number of ops:  12
compiled vars:  !0 = $array, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    7     1        INIT_FCALL_BY_NAME                                       'is_indexed_array'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $2      
          4      > RETURN                                                   $2
          5*       JMP                                                      ->11
    9     6  E > > CATCH                                       last         'Exception'
   11     7    >   INIT_METHOD_CALL                                         !1, 'getMessage'
          8        DO_FCALL                                      0  $3      
          9        ECHO                                                     $3
   12    10      > RETURN                                                   'error'
   14    11*     > RETURN                                                   null

End of function test

Function is_indexed_array:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/2hbc3
function name:  is_indexed_array
number of ops:  19
compiled vars:  !0 = $array, !1 = $v, !2 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   19     1        TYPE_CHECK                                  128  ~3      !0
          2        BOOL_NOT                                         ~4      ~3
          3      > JMPZ                                                     ~4, ->5
   20     4    > > RETURN                                                   <false>
   22     5    >   INIT_FCALL                                               'array_keys'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $5      
          8      > FE_RESET_R                                       $6      $5, ->16
          9    > > FE_FETCH_R                                       ~7      $6, !1, ->16
         10    >   ASSIGN                                                   !2, ~7
   23    11        IS_NOT_IDENTICAL                                         !2, !1
         12      > JMPZ                                                     ~9, ->15
   24    13    >   FE_FREE                                                  $6
         14      > RETURN                                                   <false>
   22    15    > > JMP                                                      ->9
         16    >   FE_FREE                                                  $6
   26    17      > RETURN                                                   <true>
   27    18*     > RETURN                                                   null

End of function is_indexed_array

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
286.69 ms | 968 KiB | 46 Q