Home  >  Article  >  Backend Development  >  Detailed explanation of PHP iterators and how to use iteration

Detailed explanation of PHP iterators and how to use iteration

php中世界最好的语言
php中世界最好的语言Original
2018-05-16 15:44:251759browse

This time I will bring you a detailed explanation of PHP iterators and how to use iterations. What are the precautions when using PHP iterators and iterations? Here are practical cases, let’s take a look.

PHP's Object-oriented engine provides a very smart feature, that is, you can use the foreach() method to take out all the properties of an object through a loop. Like the array method, the code is as follows:

class Myclass{
  public $a = 'php';
  public $b = 'onethink';
  public $c = 'thinkphp';
}
$myclass = new Myclass();
//用foreach()将对象的属性循环出来
foreach($myclass as $key.'=>'.$val){
  echo &#39;$&#39;.$key.&#39; = &#39;.$val."<br/>";
}
/*返回
  $a = php
  $b = onethink
  $c = thinkphp
*/

If you need to implement more complex behavior, you can achieve it through an iterator (iterator)

//迭代器接口
interface MyIterator{
  //函数将内部指针设置回数据开始处
  function rewind();
  //函数将判断数据指针的当前位置是否还存在更多数据
  function valid();
  //函数将返回数据指针的值
  function key();
  //函数将返回将返回当前数据指针的值
  function value();
  //函数在数据中移动数据指针的位置
  function next();
}
//迭代器类
class ObjectIterator implements MyIterator{
  private $obj;//对象
  private $count;//数据元素的数量
  private $current;//当前指针
  function construct($obj){
    $this->obj = $obj;
    $this->count = count($this->obj->data);
  }
  function rewind(){
    $this->current = 0;
  }
  function valid(){
    return $this->current < $this->count;
  }
  function key(){
    return $this->current;
  }
  function value(){
    return $this->obj->data[$this->current];
  }
  function next(){
    $this->current++;
  }
}
interface MyAggregate{
  //获取迭代器
  function getIterator();
}
class MyObject implements MyAggregate{
  public $data = array();
  function construct($in){
    $this->data = $in;
  }
  function getIterator(){
    return new ObjectIterator($this);
  }
}
//迭代器的用法
$arr = array(2,4,6,8,10);
$myobject = new MyObject($arr);
$myiterator = $myobject->getIterator();
for($myiterator->rewind();$myiterator->valid();$myiterator->next()){
  $key = $myiterator->key();
  $value = $myiterator->value();
  echo $key.&#39;=>&#39;.$value;
  echo "<br/>";
}
/*返回
  0=>2
  1=>4
  2=>6
  3=>8
  4=>10
*/

I believe you have read this article You have mastered the case method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the steps for using PHP quick sort algorithm

Detailed explanation of the steps for loading third-party class libraries in Laravel

The above is the detailed content of Detailed explanation of PHP iterators and how to use iteration. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn