Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

An example Analysis of the working principle of php iterator

Shulou Source: shulou.com Published: 2022-05-31 11:00:48 09月24日 Update

This article mainly introduces the relevant knowledge of the case analysis of the working principle of the php iterator, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on the working principle of the php iterator. Let's take a look.

working principle

1. Run the iterator::rewind () method before the first iteration.

Runs only once and returns the first element of the iterator, but the method does not return a value

2. Verify whether the first element is valid.

The verification method can be customized. If the return value is true, continue, and if the return value is false, the iteration is terminated.

3. Foreach continues to call Iterator.

Returns the key and value of the specified feature.

4. The method body of foreach

Var_dump ($key,$value)

5. After each iteration, call Iterator to determine the next element and repeat the second step.

Example

String (18) "myIterator::rewind" / / call the rewind method before the iteration starts, return to the first element of the iterator and execute $this- > position = 1; string (17) "myIterator::valid" / / verify that the current element $this- > array [1] is valid, and continue if it is valid String (19) "myIterator::current" / / returns the value of $this- > array [0] = 'firstelement' string (15) "myIterator::key" / / returns the key of the current element $this- > position = 1 int (1) / / var_dump ($key) string (12) "secondelement" / / var_dump ($value) string (16) "myIterator::next" / / move forward to the next element + + $this- > position=2 string (17) "myIterator::valid" / / verify that the current element $this- > array [2] is valid If it is valid, continue to execute. String (19) "myIterator::current" / / repeat previous step string (15) "myIterator::key" / / repeat previous step int (2) / / repeat previous step string (11) "lastelement" / / repeat previous step string (16) "myIterator::next" / / move forward to the next step Element + + $this- > position=3 string (17) "myIterator::valid" / / verify whether the current element $this- > array [3] is valid If invalid, quit traversing. This is the end of the article on "an example Analysis of the working principle of php iterators". Thank you for reading! I believe you all have a certain understanding of the knowledge of "example Analysis of the working principle of php iterator". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Iterations elements validity principles examples work methods verification case analysis analysis steps knowledge content articles elements movement operation value and simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Xiaomi MariaDB Redmi Linux