Hướng dẫn iterator function in php - hàm vòng lặp trong php

Iterator

Iterator về bản chất là một interface chuẩn được xây dựng sẵn trong PHP để bạn giễ dàng thao tác với các bộ giá trị. Cấu trúc của interface iterator trong PHP mặc định khai báo một số phương thức như sau:

interface Iterator{
  function rewind();
  function current();
  function key();
  function next();
  function valid();
}

Vậy nên lớp nào sử dụng interface interator đều phải định nghĩa các phương thức trên. Chúng ta sẽ cùng xem xét một ví dụ về việc sử dụng interator để duyệt mảng trả về từ CSDL.

class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';

ArrayIterator

ArrayIterator về bản chất nó là đối tượng được xây dựng sẵn trong thư viện PHP chuẩn và lớp này sử dụng iterface iterator.

Trong bài viết trước có đề cập đến ArrayObject cũng là đối tượng được xây dựng sẵn trong thư viện PHP chuẩn và nó có phương thức __construct() như sau:

__construct ($array, $flags=0, $iterator_class="ArrayIterator")

Chúng ta thấy đối số thứ 3 mặc định đã được khởi tạo là "ArrayIterator" để đối tượng dạng mảng có thể sử dụng đối tượng ArrayIterator thông qua phương thức getIterator(). ArrayItertor định nghĩa cho chúng ta bộ phương thức theo đúng interface iterator để thao tác với mảng. Bạn có thể xem ví dụ ở bài trước về ArrayObject với phương thức getIterator().
ArrayItertor định nghĩa cho chúng ta bộ phương thức theo đúng interface iterator để thao tác với mảng. Bạn có thể xem ví dụ ở bài trước về ArrayObject với phương thức getIterator().

Tham khảo "object-oriented programming with PHP5 _ Hasin Hayder".

5 năm trước

Người lặp lại

interface Iterator{
  function rewind();
  function current();
  function key();
  function next();
  function valid();
}

Iterator về bản chất là một giao diện Chuẩn Đ-c xây dựng sẵn trong php Để bạn giễ dàng THAO TAC VớI Các Bộ Giá Trị. Cấu trúc của giao diện iterator trong php mặc Định Khai báio một số phương thức NHư Sau:

class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';

Vậy nên lớp nào Chún ta sẽ cùnm xem xét một ví dụ về việc sử dụng interator Để Duyệt mảng trả về từ csdl.

ArrayIterator

ArrayIterator về bản chất nó là đối tượng Đan xây dựng sẵn trong thư viện php chuẩn và lớp này sử dụng iterface iterator.

__construct ($array, $flags=0, $iterator_class="ArrayIterator")

Chúng ta thấy đối số thứ 3 mặc định đã được khởi tạo là "ArrayIterator" để đối tượng dạng mảng có thể sử dụng đối tượng ArrayIterator thông qua phương thức getIterator(). ArrayItertor định nghĩa cho chúng ta bộ phương thức theo đúng interface iterator để thao tác với mảng. Bạn có thể xem ví dụ ở bài trước về ArrayObject với phương thức getIterator().
ArrayItertor định nghĩa cho chúng ta bộ phương thức theo đúng interface iterator để thao tác với mảng. Bạn có thể xem ví dụ ở bài trước về ArrayObject với phương thức getIterator().

Tham khảo "object-oriented programming with PHP5 _ Hasin Hayder".

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

iterator_to_array — Copy the iterator into an arrayCopy the iterator into an array

Description

iterator_to_array(Traversable $iterator, bool $preserve_keys = true): array(Traversable $iterator, bool $preserve_keys = true): array

Parameters

class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
0

The iterator being copied.

class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
1

Whether to use the iterator element keys as index.

If a key is an array or object, a warning will be generated.

class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
2 keys will be converted to an empty string, float keys will be truncated to their int counterpart, resource keys will generate a warning and be converted to their resource ID, and bool keys will be converted to integers. array or object, a warning will be generated.
class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
2
keys will be converted to an empty string, float keys will be truncated to their int counterpart, resource keys will generate a warning and be converted to their resource ID, and bool keys will be converted to integers.

Note: :

If this parameter is not set or set to true, duplicate keys will be overwritten. The last value with a given key will be in the returned array. Set this parameter to

class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
4 to get all the values in any case. true, duplicate keys will be overwritten. The last value with a given key will be in the returned array. Set this parameter to
class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
4
to get all the values in any case.

Return Values

An array containing the elements of the

class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
0. array containing the elements of the
class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
0.

Examples

Example #1 iterator_to_array() exampleiterator_to_array() example

class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
6

The above example will output:

array(4) {
  ["recipe"]=>
  string(8) "pancakes"
  [0]=>
  string(3) "egg"
  [1]=>
  string(4) "milk"
  [2]=>
  string(5) "flour"
}
array(4) {
  [0]=>
  string(8) "pancakes"
  [1]=>
  string(3) "egg"
  [2]=>
  string(4) "milk"
  [3]=>
  string(5) "flour"
}

jerome at yazo dot net ¶

13 years ago

class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
7

class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
8

class QueryInterator implements Iterator{
    private $result;
    private $connection;
    private $key=0;
    private $valid;
    private $data;
   
    function __construct($host, $user, $password, $dbname){
        $this->connection = mysql_connect($host,$user, $password);
        mysql_selectdb($dbname,$this->connection);
    }
   
    public function exceute($query){
        $this->result = mysql_query($query,$this->connection);
        if(mysql_num_rows($this->result) > 0){
            $this->next();
        }
    }
   
    public function current(){
        return $this->data;
    }
    public function rewind(){}
    public function key(){
        return $this->key;
    }
   
    public function next(){
        if($this->data = mysql_fetch_assoc($this->result)){
            $this->valid = true;
            $this->key +=1;
        }else{
            $this->valid = false;
        }
    }
   
    public function valid(){
        return $this->valid;
    }
}
$q = new QueryInterator("localhost","root","","test");
$q->exceute("SELECT name FROM ex1");
echo '
';
while($q->valid()){
    print_r($q->current());
    $q->next();
}
echo '
';
9

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
0

joksnet at gmail dot com ¶

7 years ago

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
1

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
2

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
3

Harry Willis ¶

7 years ago

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
4

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
5

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
3

Harry Willis ¶

wizzard351 at yahoo dot com ¶

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
7

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
8

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
9

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
3

5 months ago

chad 0x40 herballure 0x2e com ¶

interface Iterator{
  function rewind();
  function current();
  function key();
  function next();
  function valid();
}
1

14 years ago

enelar at develop-project dot ru ¶

interface Iterator{
  function rewind();
  function current();
  function key();
  function next();
  function valid();
}
2

interface Iterator{
  function rewind();
  function current();
  function key();
  function next();
  function valid();
}
3

__construct ($array, $flags=0, $iterator_class="ArrayIterator")
3