Hướng dẫn which of the following are iterator function in php - cái nào sau đây là hàm vòng lặp trong php

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

    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

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

Mike Dot Thornton tại Firstroi Dot Com ¶

Geoffrey Sneddon ¶

__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";
}
?>
3

Ẩn danh ¶

6 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

Gilles a ¶

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

Ẩn danh ¶

6 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

Gilles a ¶

Geoffrey Sneddon ¶

This is roughly equivalent to:0

Ẩn danh ¶

6 năm trước

This is roughly equivalent to:1

Gilles a ¶

6 năm trước

This is roughly equivalent to:2

This is roughly equivalent to:3

This is roughly equivalent to:4

Gilles a ¶

6 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

Gilles a ¶

8 năm trước

geompse tại gmail dot com ¶

11 năm trước

Anthony Sterling ¶

$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 lặp đi lặp lại nào sau đây?

Các loại lặp:..
Đầu vào lặp lại ..
Đầu ra lặp ..
Người chuyển tiếp Iterator ..
Trình lặp hai chiều ..
Trình lặp lại truy cập ngẫu nhiên ..

Điều nào sau đây là các iterator SPL?

SPL iterators Cây lớp ¶..
CallbackFilterIterator.RecursiveCallbackFilterIterator ..
RecursiveFilterIterator.Đ cha mẹ ..
Regexiterator.RecursiveRegexiterator ..

Là một đối tượng có thể lặp lại PHP?

PHP cho phép bạn tạo các đối tượng có thể lặp lại.Chúng có thể được sử dụng trong các vòng lặp thay vì mảng vô hướng.Ererables thường được sử dụng làm bộ sưu tập đối tượng.Chúng cho phép bạn đánh máy đối tượng đó trong khi giữ lại hỗ trợ cho vòng lặp.. These can be used within loops instead of scalar arrays. Iterables are commonly used as object collections. They allow you to typehint that object while retaining support for looping.

Loại trả lại là gì?

Có thể sử dụng cũng có thể được sử dụng như một loại trả về để chỉ ra một hàm sẽ trả về một giá trị có thể lặp lại.Nếu giá trị trả về không phải là một mảng hoặc thể hiện có thể vượt qua, một kiểu người sẽ bị ném.to indicate a function will return an iterable value. If the returned value is not an array or instance of Traversable, a TypeError will be thrown.