3v4l.org

run code in 300+ PHP versions simultaneously
<?php class IndexedArrayIterator implements Iterator { protected $data=array(); protected $keyIndex=array(); protected $reverseIndex=array(); protected $position=0; protected $startIndex=0; protected $endIndex=0; function __construct($source) { $this->data=$source; $this->reindex(); } function reindex() { // Reindexing is expensive, don't do it often // If you need to reindex every time you search, you loose the advantage // If you add elements, you must reindex $this->keyIndex = array_keys($this->data); $this->reverseIndex = array_flip($this->keyIndex); } function setStartKey($start) { $this->startIndex=$this->reverseIndex[$start]; } function setEndKey($end) { $this->endIndex=$this->reverseIndex[$end]; } ///// /// Iterator Interface // #[\ReturnTypeWillChange] public function rewind() { $this->position=$this->startIndex; } #[\ReturnTypeWillChange] public function current() { if($this->valid()) return $this->data[$this->key()]; else return NULL; } #[\ReturnTypeWillChange] public function key() { if($this->valid()) return $this->keyIndex[$this->position]; else return NULL; } #[\ReturnTypeWillChange] public function next() { ++$this->position; } #[\ReturnTypeWillChange] public function valid() { if($this->position > $this->endIndex) return FALSE; return isset($this->keyIndex[$this->position]); } } $iterator=new IndexedArrayIterator(array( 'one'=>'Item One', 'two'=>'Item Two', 'twoandhalf'=>'Item Two And Half', 'three'=>'Item Three', 'four'=>'Item Four', )); $iterator->setStartKey('two'); $iterator->setEndKey('three'); foreach($iterator as $key=>$value) { echo(''.$key.': '.$value."\r\n"); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 20
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/EP0fL
function name:  (null)
number of ops:  22
compiled vars:  !0 = $iterator, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   DECLARE_CLASS                                            'indexedarrayiterator'
   73     1        NEW                                              $3      'IndexedArrayIterator'
   74     2        SEND_VAL_EX                                              <array>
          3        DO_FCALL                                      0          
   73     4        ASSIGN                                                   !0, $3
   81     5        INIT_METHOD_CALL                                         !0, 'setStartKey'
          6        SEND_VAL_EX                                              'two'
          7        DO_FCALL                                      0          
   82     8        INIT_METHOD_CALL                                         !0, 'setEndKey'
          9        SEND_VAL_EX                                              'three'
         10        DO_FCALL                                      0          
   84    11      > FE_RESET_R                                       $8      !0, ->20
         12    > > FE_FETCH_R                                       ~9      $8, !1, ->20
         13    >   ASSIGN                                                   !2, ~9
   85    14        CONCAT                                           ~11     '', !2
         15        CONCAT                                           ~12     ~11, '%3A+'
         16        CONCAT                                           ~13     ~12, !1
         17        CONCAT                                           ~14     ~13, '%0D%0A'
         18        ECHO                                                     ~14
   84    19      > JMP                                                      ->12
         20    >   FE_FREE                                                  $8
   86    21      > RETURN                                                   1

Class IndexedArrayIterator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EP0fL
function name:  __construct
number of ops:  6
compiled vars:  !0 = $source
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   15     1        ASSIGN_OBJ                                               'data'
          2        OP_DATA                                                  !0
   16     3        INIT_METHOD_CALL                                         'reindex'
          4        DO_FCALL                                      0          
   17     5      > RETURN                                                   null

End of function __construct

Function reindex:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EP0fL
function name:  reindex
number of ops:  13
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   INIT_FCALL                                               'array_keys'
          1        FETCH_OBJ_R                                      ~1      'data'
          2        SEND_VAL                                                 ~1
          3        DO_ICALL                                         $2      
          4        ASSIGN_OBJ                                               'keyIndex'
          5        OP_DATA                                                  $2
   24     6        INIT_FCALL                                               'array_flip'
          7        FETCH_OBJ_R                                      ~4      'keyIndex'
          8        SEND_VAL                                                 ~4
          9        DO_ICALL                                         $5      
         10        ASSIGN_OBJ                                               'reverseIndex'
         11        OP_DATA                                                  $5
   25    12      > RETURN                                                   null

End of function reindex

Function setstartkey:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EP0fL
function name:  setStartKey
number of ops:  6
compiled vars:  !0 = $start
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   28     1        FETCH_OBJ_R                                      ~2      'reverseIndex'
          2        FETCH_DIM_R                                      ~3      ~2, !0
          3        ASSIGN_OBJ                                               'startIndex'
          4        OP_DATA                                                  ~3
   29     5      > RETURN                                                   null

End of function setstartkey

Function setendkey:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EP0fL
function name:  setEndKey
number of ops:  6
compiled vars:  !0 = $end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   32     1        FETCH_OBJ_R                                      ~2      'reverseIndex'
          2        FETCH_DIM_R                                      ~3      ~2, !0
          3        ASSIGN_OBJ                                               'endIndex'
          4        OP_DATA                                                  ~3
   33     5      > RETURN                                                   null

End of function setendkey

Function rewind:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EP0fL
function name:  rewind
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   FETCH_OBJ_R                                      ~1      'startIndex'
          1        ASSIGN_OBJ                                               'position'
          2        OP_DATA                                                  ~1
   44     3      > RETURN                                                   null

End of function rewind

Function current:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EP0fL
function name:  current
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   INIT_METHOD_CALL                                         'valid'
          1        DO_FCALL                                      0  $0      
          2      > JMPZ                                                     $0, ->9
          3    >   INIT_METHOD_CALL                                         'key'
          4        DO_FCALL                                      0  $2      
          5        FETCH_OBJ_R                                      ~1      'data'
          6        FETCH_DIM_R                                      ~3      ~1, $2
          7      > RETURN                                                   ~3
          8*       JMP                                                      ->10
   49     9    > > RETURN                                                   null
   50    10*     > RETURN                                                   null

End of function current

Function key:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EP0fL
function name:  key
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   INIT_METHOD_CALL                                         'valid'
          1        DO_FCALL                                      0  $0      
          2      > JMPZ                                                     $0, ->8
          3    >   FETCH_OBJ_R                                      ~2      'position'
          4        FETCH_OBJ_R                                      ~1      'keyIndex'
          5        FETCH_DIM_R                                      ~3      ~1, ~2
          6      > RETURN                                                   ~3
          7*       JMP                                                      ->9
   55     8    > > RETURN                                                   null
   56     9*     > RETURN                                                   null

End of function key

Function next:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EP0fL
function name:  next
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   PRE_INC_OBJ                                              'position'
   61     1      > RETURN                                                   null

End of function next

Function valid:
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
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EP0fL
function name:  valid
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   FETCH_OBJ_R                                      ~0      'position'
          1        FETCH_OBJ_R                                      ~1      'endIndex'
          2        IS_SMALLER                                               ~1, ~0
          3      > JMPZ                                                     ~2, ->5
          4    > > RETURN                                                   <false>
   66     5    >   FETCH_OBJ_R                                      ~4      'position'
          6        FETCH_OBJ_IS                                     ~3      'keyIndex'
          7        ISSET_ISEMPTY_DIM_OBJ                         0  ~5      ~3, ~4
          8      > RETURN                                                   ~5
   67     9*     > RETURN                                                   null

End of function valid

End of class IndexedArrayIterator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.56 ms | 1421 KiB | 17 Q