Reputation: 67698
I want to use an associative array with the PHP iterator:
http://php.net/manual/en/class.iterator.php
is it possible?
I defined these methods:
public function rewind(){
reset($this->_arr);
$this->_position = key($this->_arr);
}
public function current(){
return $this->_arr[$this->_position];
}
public function key(){
return $this->_position;
}
public function next(){
++$this->_position;
}
public function valid(){
return isset($this->_arr[$this->_position]);
}
the problem is it doesn't iterate correctly. I only get one element.
I think it's because of the ++$this->_position
code in the next() method which doesn't have any effect because _position is a string (key of the associative array).
so how can I go to the next element of this type of array?
Upvotes: 19
Views: 6072
Reputation: 2489
I know this is an old question but it can be as simple as
public function current()
{
return $this->array[array_keys($this->array)[$this->position]];
}
public function next()
{
++$this->position;
}
public function key()
{
return array_keys($this->array)[$this->position];
}
public function valid()
{
return array_key_exists($this->position, array_keys($this->array)[$this->position]);
}
public function rewind()
{
$this->position = 0;
}
I know a similar answer was posted by @zerkms but imho that doesnt work if the object is already constructed and you have functionality that extends the array
Upvotes: 0
Reputation: 11
class MyItrator implements Iterator
{
private $_arr;
public function __construct(array $arr)
{
$this->_arr = $arr;
}
public function rewind()
{
reset($this->_arr);
}
public function current()
{
return current($this->_arr);
}
public function key()
{
return key($this->_arr);
}
public function next()
{
next($this->_arr);
}
public function valid()
{
return isset($this->_arr[key($this->_arr)]);
}
}
Upvotes: 1
Reputation: 31823
function rewind() {
reset($this->_arr);
}
function current() {
return current($this->_arr);
}
function key() {
return key($this->_arr);
}
function next() {
next($this->_arr);
}
function valid() {
return key($this->_arr) !== null;
}
Upvotes: 39
Reputation: 16055
Why not creating an ArrayObject
from Your associative Array
? Then You can getIterator()
from this ArrayObject
and call key()
, next()
etc on it as You want...
Some example:
$array = array('one' => 'ONE', 'two' => 'TWO', 'three' = 'THREE');
// create ArrayObject and get it's iterator
$ao = new ArrayObject($my_array);
$it = $ao->getIterator();
// looping
while($it->valid()) {
echo "Under key {$it->key()} is value {$it->current()}";
$it->next();
}
Upvotes: 5
Reputation: 255005
class myIterator implements Iterator {
private $position = 0;
private $keys;
public function __construct(array $arr) {
$this->array = $arr;
$this->keys = array_keys($arr);
$this->position = 0;
}
function rewind() {
$this->position = 0;
}
function current() {
return $this->array[$this->key()];
}
function key() {
return $this->keys[$this->position];
}
function next() {
++$this->position;
}
function valid() {
return isset($this->keys[$this->position]);
}
}
$it = new myIterator(array(
'a' => "firstelement",
'b' => "secondelement",
'c' => "lastelement",
));
foreach($it as $key => $value) {
var_dump($key, $value);
echo "\n";
}
Upvotes: 2