Hướng dẫn what is an iterator in php? - trình lặp trong php là gì?

Giao diện vòng lặp

(Php 5, Php 7, Php 8)

Giới thiệu

Giao diện cho các trình lặp bên ngoài hoặc các đối tượng có thể tự lặp lại trong nội bộ.

Bản tóm tắt giao diện

Xác định trước

PHP đã cung cấp một số trình lặp trong nhiều nhiệm vụ hàng ngày. Xem SPL Iterators cho một danh sách.

Ví dụ

Ví dụ số 1 sử dụng cơ bản

Ví dụ này chứng minh trong đó các phương thức đặt hàng được gọi khi sử dụng foreach với một trình lặp.

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  

    public function

__construct() {
        
$this->position 0;
    }

    public function

rewind(): void {
        
var_dump(__METHOD__);
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current() {
        
var_dump(__METHOD__);
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key() {
        
var_dump(__METHOD__);
        return 
$this->position;
    }

    public function

next(): void {
        
var_dump(__METHOD__);
        ++
$this->position;
    }

    public function

valid(): bool {
        
var_dump(__METHOD__);
        return isset(
$this->array[$this->position]);
    }
}
$it = new myIterator;

foreach(

$it as $key => $value) {
    
var_dump($key$value);
    echo 
"\n";
}
?>

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như:

string(18) "myIterator::rewind"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
int(0)
string(12) "firstelement"

string(16) "myIterator::next"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
int(1)
string(13) "secondelement"

string(16) "myIterator::next"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
int(2)
string(11) "lastelement"

string(16) "myIterator::next"
string(17) "myIterator::valid"

Mục lục

  • Iterator :: hiện tại - trả về phần tử hiện tại
  • Iterator :: phím - Trả lại khóa của phần tử hiện tại
  • Iterator :: Tiếp theo - Chuyển tiếp đến phần tử tiếp theo
  • Iterator :: tua lại - tua lại trình lặp lại phần tử đầu tiên
  • Iterator :: hợp lệ - kiểm tra xem vị trí hiện tại có hợp lệ không

Robert_e_lee tại Dell Dot Com ¶

12 năm trước

Order of operations when using a foreach loop:

1. Before the first iteration of the loop, Iterator::rewind() is called.
2. Before each iteration of the loop, Iterator::valid() is called.
3a. It Iterator::valid() returns false, the loop is terminated.
3b. If Iterator::valid() returns true, Iterator::current() and
Iterator::key() are called.
4. The loop body is evaluated.
5. After each iteration of the loop, Iterator::next() is called and we repeat from step 2 above.

This is roughly equivalent to:

$it->rewind();

while (

$it->valid())
{
   
$key = $it->key();
   
$value = $it->current();// ...$it->next();
}
?>

The loop isn't terminated until Iterator::valid() returns false or the body of the loop executes a break statement.

The only two methods that are always executed are Iterator::rewind() and Iterator::valid() (unless rewind throws an exception).

The Iterator::next() method need not return anything. It is defined as returning void. On the other hand, sometimes it is convenient for this method to return something, in which case you can do so if you want.

If your iterator is doing something expensive, like making a database query and iterating over the result set, the best place to make the query is probably in the Iterator::rewind() implementation.

In this case, the construction of the iterator itself can be cheap, and after construction you can continue to set the properties of the query all the way up to the beginning of the foreach loop since the
Iterator::rewind() method isn't called until then.

Things to keep in mind when making a database result set iterator:

* Make sure you close your cursor or otherwise clean up any previous query at the top of the rewind method. Otherwise your code will break if the same iterator is used in two consecutive foreach loops when the first loop terminates with a break statement before all the results are iterated over.

* Make sure your rewind() implementation tries to grab the first result so that the subsequent call to valid() will know whether or not the result set is empty. I do this by explicitly calling next() from the end of my rewind() implementation.

* For things like result set iterators, there really isn't always a "key" that you can return, unless you know you have a scalar primary key column in the query. Unfortunately, there will be cases where either the iterator doesn't know the primary key column because it isn't providing the query, the nature of the query is such that a primary key isn't applicable, the iterator is iterating over a table that doesn't have one, or the iterator is iterating over a table that has a compound primary key. In these cases, key() can return either:
the row index (based on a simple counter that you provide), or can simply return null.

Iterators can also be used to:

* iterate over the lines of a file or rows of a CSV file
* iterate over the characters of a string
* iterate over the tokens in an input stream
* iterate over the matches returned by an xpath expression
* iterate over the matches returned by a regexp
* iterate over the files in a folder
* etc...

Rocketinabog tại Techno-Monks Dot Net

13 năm trước

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
0

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
1

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
2

fetidfrog tại gmail dot com ¶

10 năm trước

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
3

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
4

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
5

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
6

Mike Dot Thornton tại Firstroi Dot Com ¶

13 năm trước

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
7

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
8

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
9

    public function0

    public function1

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
6

fetidfrog tại gmail dot com ¶

13 năm trước

    public function3

    public function4

    public function5

    public function6

fetidfrog tại gmail dot com ¶

10 năm trước

    public function7

Mike Dot Thornton tại Firstroi Dot Com ¶

Geoffrey Sneddon ¶

    public function8

    public function9

__construct() {
        
$this->position 0;
    }

    public function

rewind(): void {
        
var_dump(__METHOD__);
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current() {
        
var_dump(__METHOD__);
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key() {
        
var_dump(__METHOD__);
        return 
$this->position;
    }

    public function

next(): void {
        
var_dump(__METHOD__);
        ++
$this->position;
    }

    public function

valid(): bool {
        
var_dump(__METHOD__);
        return isset(
$this->array[$this->position]);
    }
}
$it = new myIterator;

foreach(

$it as $key => $value) {
    
var_dump($key$value);
    echo 
"\n";
}
?>
0

__construct() {
        
$this->position 0;
    }

    public function

rewind(): void {
        
var_dump(__METHOD__);
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current() {
        
var_dump(__METHOD__);
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key() {
        
var_dump(__METHOD__);
        return 
$this->position;
    }

    public function

next(): void {
        
var_dump(__METHOD__);
        ++
$this->position;
    }

    public function

valid(): bool {
        
var_dump(__METHOD__);
        return isset(
$this->array[$this->position]);
    }
}
$it = new myIterator;

foreach(

$it as $key => $value) {
    
var_dump($key$value);
    echo 
"\n";
}
?>
1

__construct() {
        
$this->position 0;
    }

    public function

rewind(): void {
        
var_dump(__METHOD__);
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current() {
        
var_dump(__METHOD__);
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key() {
        
var_dump(__METHOD__);
        return 
$this->position;
    }

    public function

next(): void {
        
var_dump(__METHOD__);
        ++
$this->position;
    }

    public function

valid(): bool {
        
var_dump(__METHOD__);
        return isset(
$this->array[$this->position]);
    }
}
$it = new myIterator;

foreach(

$it as $key => $value) {
    
var_dump($key$value);
    echo 
"\n";
}
?>
2

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
6

Gilles a ¶

8 năm trước

__construct() {
        
$this->position 0;
    }

    public function

rewind(): void {
        
var_dump(__METHOD__);
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current() {
        
var_dump(__METHOD__);
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key() {
        
var_dump(__METHOD__);
        return 
$this->position;
    }

    public function

next(): void {
        
var_dump(__METHOD__);
        ++
$this->position;
    }

    public function

valid(): bool {
        
var_dump(__METHOD__);
        return isset(
$this->array[$this->position]);
    }
}
$it = new myIterator;

foreach(

$it as $key => $value) {
    
var_dump($key$value);
    echo 
"\n";
}
?>
4

__construct() {
        
$this->position 0;
    }

    public function

rewind(): void {
        
var_dump(__METHOD__);
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current() {
        
var_dump(__METHOD__);
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key() {
        
var_dump(__METHOD__);
        return 
$this->position;
    }

    public function

next(): void {
        
var_dump(__METHOD__);
        ++
$this->position;
    }

    public function

valid(): bool {
        
var_dump(__METHOD__);
        return isset(
$this->array[$this->position]);
    }
}
$it = new myIterator;

foreach(

$it as $key => $value) {
    
var_dump($key$value);
    echo 
"\n";
}
?>
5

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
6

Ẩn danh ¶

13 năm trước

__construct() {
        
$this->position 0;
    }

    public function

rewind(): void {
        
var_dump(__METHOD__);
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current() {
        
var_dump(__METHOD__);
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key() {
        
var_dump(__METHOD__);
        return 
$this->position;
    }

    public function

next(): void {
        
var_dump(__METHOD__);
        ++
$this->position;
    }

    public function

valid(): bool {
        
var_dump(__METHOD__);
        return isset(
$this->array[$this->position]);
    }
}
$it = new myIterator;

foreach(

$it as $key => $value) {
    
var_dump($key$value);
    echo 
"\n";
}
?>
7

__construct() {
        
$this->position 0;
    }

    public function

rewind(): void {
        
var_dump(__METHOD__);
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current() {
        
var_dump(__METHOD__);
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key() {
        
var_dump(__METHOD__);
        return 
$this->position;
    }

    public function

next(): void {
        
var_dump(__METHOD__);
        ++
$this->position;
    }

    public function

valid(): bool {
        
var_dump(__METHOD__);
        return isset(
$this->array[$this->position]);
    }
}
$it = new myIterator;

foreach(

$it as $key => $value) {
    
var_dump($key$value);
    echo 
"\n";
}
?>
8

__construct() {
        
$this->position 0;
    }

    public function

rewind(): void {
        
var_dump(__METHOD__);
        
$this->position 0;
    }
#[ReturnTypeWillChange]
    
public function current() {
        
var_dump(__METHOD__);
        return 
$this->array[$this->position];
    }
#[ReturnTypeWillChange]
    
public function key() {
        
var_dump(__METHOD__);
        return 
$this->position;
    }

    public function

next(): void {
        
var_dump(__METHOD__);
        ++
$this->position;
    }

    public function

valid(): bool {
        
var_dump(__METHOD__);
        return isset(
$this->array[$this->position]);
    }
}
$it = new myIterator;

foreach(

$it as $key => $value) {
    
var_dump($key$value);
    echo 
"\n";
}
?>
9

Order of operations when using a foreach loop:0

fetidfrog tại gmail dot com ¶

10 năm trước

Order of operations when using a foreach loop:1

Order of operations when using a foreach loop:2

Order of operations when using a foreach loop:3

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
6

Mike Dot Thornton tại Firstroi Dot Com ¶

Geoffrey Sneddon ¶

Order of operations when using a foreach loop:5

Order of operations when using a foreach loop:6

Order of operations when using a foreach loop:7

Order of operations when using a foreach loop:8

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
6

Gilles a ¶

8 năm trước

1. Before the first iteration of the loop, Iterator::rewind() is called.
2. Before each iteration of the loop, Iterator::valid() is called.
3a. It Iterator::valid() returns false, the loop is terminated.
3b. If Iterator::valid() returns true, Iterator::current() and
Iterator::key() are called.
4. The loop body is evaluated.
5. After each iteration of the loop, Iterator::next() is called and we repeat from step 2 above.
0

1. Before the first iteration of the loop, Iterator::rewind() is called.
2. Before each iteration of the loop, Iterator::valid() is called.
3a. It Iterator::valid() returns false, the loop is terminated.
3b. If Iterator::valid() returns true, Iterator::current() and
Iterator::key() are called.
4. The loop body is evaluated.
5. After each iteration of the loop, Iterator::next() is called and we repeat from step 2 above.
1

1. Before the first iteration of the loop, Iterator::rewind() is called.
2. Before each iteration of the loop, Iterator::valid() is called.
3a. It Iterator::valid() returns false, the loop is terminated.
3b. If Iterator::valid() returns true, Iterator::current() and
Iterator::key() are called.
4. The loop body is evaluated.
5. After each iteration of the loop, Iterator::next() is called and we repeat from step 2 above.
2

1. Before the first iteration of the loop, Iterator::rewind() is called.
2. Before each iteration of the loop, Iterator::valid() is called.
3a. It Iterator::valid() returns false, the loop is terminated.
3b. If Iterator::valid() returns true, Iterator::current() and
Iterator::key() are called.
4. The loop body is evaluated.
5. After each iteration of the loop, Iterator::next() is called and we repeat from step 2 above.
3

1. Before the first iteration of the loop, Iterator::rewind() is called.
2. Before each iteration of the loop, Iterator::valid() is called.
3a. It Iterator::valid() returns false, the loop is terminated.
3b. If Iterator::valid() returns true, Iterator::current() and
Iterator::key() are called.
4. The loop body is evaluated.
5. After each iteration of the loop, Iterator::next() is called and we repeat from step 2 above.
4

1. Before the first iteration of the loop, Iterator::rewind() is called.
2. Before each iteration of the loop, Iterator::valid() is called.
3a. It Iterator::valid() returns false, the loop is terminated.
3b. If Iterator::valid() returns true, Iterator::current() and
Iterator::key() are called.
4. The loop body is evaluated.
5. After each iteration of the loop, Iterator::next() is called and we repeat from step 2 above.
5

1. Before the first iteration of the loop, Iterator::rewind() is called.
2. Before each iteration of the loop, Iterator::valid() is called.
3a. It Iterator::valid() returns false, the loop is terminated.
3b. If Iterator::valid() returns true, Iterator::current() and
Iterator::key() are called.
4. The loop body is evaluated.
5. After each iteration of the loop, Iterator::next() is called and we repeat from step 2 above.
6

1. Before the first iteration of the loop, Iterator::rewind() is called.
2. Before each iteration of the loop, Iterator::valid() is called.
3a. It Iterator::valid() returns false, the loop is terminated.
3b. If Iterator::valid() returns true, Iterator::current() and
Iterator::key() are called.
4. The loop body is evaluated.
5. After each iteration of the loop, Iterator::next() is called and we repeat from step 2 above.
7

1. Before the first iteration of the loop, Iterator::rewind() is called.
2. Before each iteration of the loop, Iterator::valid() is called.
3a. It Iterator::valid() returns false, the loop is terminated.
3b. If Iterator::valid() returns true, Iterator::current() and
Iterator::key() are called.
4. The loop body is evaluated.
5. After each iteration of the loop, Iterator::next() is called and we repeat from step 2 above.
8

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
6

Ẩn danh ¶

Geoffrey Sneddon ¶

This is roughly equivalent to:0

Gilles a ¶

8 năm trước

This is roughly equivalent to:1

Ẩn danh ¶

8 năm trước

This is roughly equivalent to:2

This is roughly equivalent to:3

This is roughly equivalent to:4

Ẩn danh ¶

8 năm trước

This is roughly equivalent to:5

This is roughly equivalent to:6

This is roughly equivalent to:7

This is roughly equivalent to:8

This is roughly equivalent to:9

$it->rewind();0

Ẩn danh ¶

6 năm trước

$it->rewind();1

geompse tại gmail dot com ¶

11 năm trước

$it->rewind();2

$it->rewind();3

$it->rewind();4

class myIterator implements Iterator {
    private 
$position 0;
    private 
$array = array(
        
"firstelement",
        
"secondelement",
        
"lastelement",
    );  
1

$it->rewind();6

Chức năng Iterator là gì?

Trong JavaScript, một trình lặp là một đối tượng xác định một chuỗi và có khả năng là giá trị trả về khi chấm dứt. Cụ thể, một trình lặp là bất kỳ đối tượng nào thực hiện giao thức iterator bằng cách có một phương thức tiếp theo () trả về một đối tượng có hai thuộc tính: giá trị. Giá trị tiếp theo trong chuỗi lặp.an object which defines a sequence and potentially a return value upon its termination. Specifically, an iterator is any object which implements the Iterator protocol by having a next() method that returns an object with two properties: value. The next value in the iteration sequence.

Một người lặp là gì và tại sao iterator là cần thiết?

Trình lặp là một đối tượng (giống như một con trỏ) trỏ đến một phần tử bên trong thùng chứa. Chúng ta có thể sử dụng trình lặp để di chuyển qua nội dung của container. Chúng có thể được hình dung như một cái gì đó tương tự như một con trỏ trỏ đến một số vị trí và chúng ta có thể truy cập nội dung tại vị trí cụ thể đó bằng cách sử dụng chúng.an object (like a pointer) that points to an element inside the container. We can use iterators to move through the contents of the container. They can be visualized as something similar to a pointer pointing to some location and we can access the content at that particular location using them.

Trình lặp PHP quan trọng như thế nào trong một ứng dụng?

Trình lặp khuyến khích bạn xử lý dữ liệu lặp đi lặp lại, thay vì đệm nó trong bộ nhớ.Mặc dù có thể làm điều này mà không cần tererators, nhưng sự trừu tượng mà họ cung cấp che giấu việc thực hiện giúp chúng thực sự dễ sử dụng.encourage you to process data iteratively, instead of buffering it in memory. While it is possible to do this without iterators, the abstractions they provide hide the implementation which makes them really easy to use.

Là một mảng có thể lặp lại?

Các đối tượng có thể hiểu được là một khái quát của các mảng.Đó là một khái niệm cho phép chúng tôi tạo bất kỳ đối tượng nào có thể sử dụng được trong vòng lặp cho..of.Tất nhiên, mảng là điều đó.