【问题标题】:Using foreach over an object implementing ArrayAccess and Iterator在实现 ArrayAccess 和 Iterator 的对象上使用 foreach
【发布时间】:2012-04-15 21:58:36
【问题描述】:

有没有办法迭代实现 ArrayAccess 和 Iterator 接口的对象键?数组访问是一种魅力,但我不能在那些对我有很大帮助的对象上使用 foreach。可能吗?到目前为止我有这样的代码:

<?php
class IteratorTest implements ArrayAccess, Iterator {
  private $pointer = 0;

  public function offsetExists($index) {
    return isset($this->objects[$index]);
  }

  public function offsetGet($index) {
    return $this->objects[$index];
  }

  public function offsetSet($index, $newValue) {
    $this->objects[$index] = $newValue;
  }

  public function offsetUnset($index) {
    unset($this->objects[$index]);
  }

  public function key() {
    return $this->pointer;
  }

  public function current() {
    return $this->objects[$this -> pointer];
  }

  public function next() {
    $this->pointer++;
  }

  public function rewind() {
    $this->pointer = 0;
  }

  public function seek($position) {
    $this->pointer = $position;
  }

  public function valid() {
    return isset($this->objects[$this -> pointer]);
  }
}

$it = new IteratorTest();

$it['one'] = 1;
$it['two'] = 2;

foreach ($it as $k => $v) {
  echo "$k: $v\n";
}

// expected result:
// one: 1
// two: 2

感谢任何帮助和提示。

【问题讨论】:

    标签: php foreach iterator arrayaccess


    【解决方案1】:

    我用它来实现迭代器。也许你可以适应你的代码;)

    class ModelList implements Iterator{
    public $list;
    private $index = 0;
    public $nb;
    public $nbTotal;
    
    /**
     * list navigation
     */
    public function rewind(){$this->index = 0;}
    public function current(){$k = array_keys($this->list);$var = $this->list[$k[$this->index]];return $var;}
    public function key(){$k = array_keys($this->list);$var = $k[$this->index];return $var;}
    public function next(){$k = array_keys($this->list);if (isset($k[++$this->index])) {$var = $this->list[$k[$this->index]];return $var;} else {return false;}}
    public function valid(){$k = array_keys($this->list);$var = isset($k[$this->index]);return $var;}
    /**
     * 
     * Constructor
     */
    public function __construct() {
        $this->list = array();
        $this->nb = 0;
        $this->nbTotal = 0;
        return $this;
    }
    }
    

    【讨论】:

      【解决方案2】:
      while ($it->valid()) {
          echo $it->key().' '.$it->current();
          $it->next();
      }
      

      这将是我的方法,但是,这个函数看起来不太好:

       public function next() {
          $this->pointer++;
       }
      

      增加“一”不太可能给您“二”。尝试this question 答案中的代码以获取下一个数组键:

      $keys = array_keys($this->objects);
      $position = array_search($this->key(), $keys);
      if (isset($keys[$position + 1])) {
          $this->pointer = $keys[$position + 1];
      } else {
          $this->pointer = false;
      }
      

      【讨论】:

      • 感谢您的提示,但我需要这些对象使用 foreach 工作,因为它们被作为参数传递给我无法修改的其他函数。我对其进行了一些修改,它似乎可以工作:http://ideone.com/rZjim
      猜你喜欢
      • 2016-05-15
      • 2011-07-14
      • 1970-01-01
      • 2020-06-26
      • 2015-01-30
      • 2014-03-16
      • 2011-04-12
      • 2012-03-30
      • 2020-12-22
      相关资源
      最近更新 更多