Hướng dẫn php multipleiterator - nhiều trình lặp php

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.

next() and rewind() will be called on all attached iterators in every case.

$it1

= new ArrayIterator(array(1,2,3));
$it2 = new ArrayIterator(array(4,5,6));$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);$multipleIterator->attachIterator($it1, 1);
$multipleIterator->attachIterator($it2, 'second');

foreach (

$multipleIterator as $key => $value) {
    echo
"Key\n"; var_dump($key);
    echo
"Value\n"; var_dump($value);
    echo
"---next---\n";
}
?>

Result with PHP 5.5.0 and up:

Key
array(2) {
  [1]=>
  int(0)
  ["second"]=>
  int(0)
}
Value
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(4)
}
---next---
Key
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(1)
}
Value
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(5)
}
---next---
Key
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(2)
}
Value
array(2) {
  [1]=>
  int(3)
  ["second"]=>
  int(6)
}
---next---

Note that PHP 5.4 and 5.3 do not support accessing the key() values in foreach loops because they expect them to not be an array - doing so will cause "Warning: Illegal type returned from MultipleIterator::key()" and the result of (int)0 as the key for all iterations.

Without the MultipleIterator::MIT_KEYS_ASSOC flag, the MultipleIterator will create numeric indices based on the order of attachment.

Giao diện vòng lặp

(Php 5, Php 7, Php 8)

Nội phân Chính showShow

  • Giao diện vòng lặp
  • (Php 5, Php 7, Php 8)
  • Nội phân Chính show
  • Giới thiệu
  • Bản tóm tắt giao diện
  • Chức năng lặp đi lặp lại nào sau đây?
  • Là một đối tượng có thể lặp lại PHP?
  • Loại trả lại là gì?
  • Trình lặp PHP quan trọng như thế nào trong một ứng dụng?

(Php 5, Php 7, Php 8)

Nội phân Chính show

Nội phân Chính show

Giới thiệu

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

Xác định trước

Mục lục

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ộ.

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

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.

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"

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

  • Xác định trước
  • Mục lục
  • 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ộ.
  • 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.

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.0

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

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.2

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.3

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.4

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.5

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

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.6

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.7

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.8

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

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.9

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.0

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.1

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.2

Iterator :: hợp lệ - kiểm tra xem vị trí hiện tại có hợp lệ không

Iterator :: phím - Trả lại khóa của phần tử hiện tại

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.3

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.4

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.5

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.6

Iterator :: Tiếp theo - Chuyển tiếp đến phần tử tiếp theo

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.2

Iterator :: tua lại - tua lại trình lặp lại phần tử đầu tiên

Iterator :: phím - Trả lại khóa của phần tử hiện tại

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.9

next() and rewind() will be called on all attached iterators in every case.0

next() and rewind() will be called on all attached iterators in every case.1

next() and rewind() will be called on all attached iterators in every case.2

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

next() and rewind() will be called on all attached iterators in every case.3

next() and rewind() will be called on all attached iterators in every case.4

next() and rewind() will be called on all attached iterators in every case.5

next() and rewind() will be called on all attached iterators in every case.6

next() and rewind() will be called on all attached iterators in every case.7

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.2

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 ¶

next() and rewind() will be called on all attached iterators in every case.9

12 năm trước

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.1

0

1

Rocketinabog tại Techno-Monks Dot Net

13 năm trước

Iterator :: phím - Trả lại khóa của phần tử hiện tại

3

4

5

6

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

7

8

9

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.2

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 ¶

$it1 1

$it1 2

$it1 3

$it1 4

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.2

12 năm trước

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.1

Rocketinabog tại Techno-Monks Dot Net

$it1 7

$it1 8

$it1 9

= new ArrayIterator(array(1,2,3));
$it2 = new ArrayIterator(array(4,5,6));$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);$multipleIterator->attachIterator($it1, 1);
$multipleIterator->attachIterator($it2, 'second');

foreach (

$multipleIterator as $key => $value) {
    echo
"Key\n"; var_dump($key);
    echo
"Value\n"; var_dump($value);
    echo
"---next---\n";
}
?>

Result with PHP 5.5.0 and up:

Key
array(2) {
  [1]=>
  int(0)
  ["second"]=>
  int(0)
}
Value
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(4)
}
---next---
Key
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(1)
}
Value
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(5)
}
---next---
Key
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(2)
}
Value
array(2) {
  [1]=>
  int(3)
  ["second"]=>
  int(6)
}
---next---

Note that PHP 5.4 and 5.3 do not support accessing the key() values in foreach loops because they expect them to not be an array - doing so will cause "Warning: Illegal type returned from MultipleIterator::key()" and the result of (int)0 as the key for all iterations.

Without the MultipleIterator::MIT_KEYS_ASSOC flag, the MultipleIterator will create numeric indices based on the order of attachment.

0

= new ArrayIterator(array(1,2,3));
$it2 = new ArrayIterator(array(4,5,6));$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);$multipleIterator->attachIterator($it1, 1);
$multipleIterator->attachIterator($it2, 'second');

foreach (

$multipleIterator as $key => $value) {
    echo
"Key\n"; var_dump($key);
    echo
"Value\n"; var_dump($value);
    echo
"---next---\n";
}
?>

Result with PHP 5.5.0 and up:

Key
array(2) {
  [1]=>
  int(0)
  ["second"]=>
  int(0)
}
Value
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(4)
}
---next---
Key
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(1)
}
Value
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(5)
}
---next---
Key
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(2)
}
Value
array(2) {
  [1]=>
  int(3)
  ["second"]=>
  int(6)
}
---next---

Note that PHP 5.4 and 5.3 do not support accessing the key() values in foreach loops because they expect them to not be an array - doing so will cause "Warning: Illegal type returned from MultipleIterator::key()" and the result of (int)0 as the key for all iterations.

Without the MultipleIterator::MIT_KEYS_ASSOC flag, the MultipleIterator will create numeric indices based on the order of attachment.

1

= new ArrayIterator(array(1,2,3));
$it2 = new ArrayIterator(array(4,5,6));$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);$multipleIterator->attachIterator($it1, 1);
$multipleIterator->attachIterator($it2, 'second');

foreach (

$multipleIterator as $key => $value) {
    echo
"Key\n"; var_dump($key);
    echo
"Value\n"; var_dump($value);
    echo
"---next---\n";
}
?>

Result with PHP 5.5.0 and up:

Key
array(2) {
  [1]=>
  int(0)
  ["second"]=>
  int(0)
}
Value
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(4)
}
---next---
Key
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(1)
}
Value
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(5)
}
---next---
Key
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(2)
}
Value
array(2) {
  [1]=>
  int(3)
  ["second"]=>
  int(6)
}
---next---

Note that PHP 5.4 and 5.3 do not support accessing the key() values in foreach loops because they expect them to not be an array - doing so will cause "Warning: Illegal type returned from MultipleIterator::key()" and the result of (int)0 as the key for all iterations.

Without the MultipleIterator::MIT_KEYS_ASSOC flag, the MultipleIterator will create numeric indices based on the order of attachment.

2

= new ArrayIterator(array(1,2,3));
$it2 = new ArrayIterator(array(4,5,6));$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);$multipleIterator->attachIterator($it1, 1);
$multipleIterator->attachIterator($it2, 'second');

foreach (

$multipleIterator as $key => $value) {
    echo
"Key\n"; var_dump($key);
    echo
"Value\n"; var_dump($value);
    echo
"---next---\n";
}
?>

Result with PHP 5.5.0 and up:

Key
array(2) {
  [1]=>
  int(0)
  ["second"]=>
  int(0)
}
Value
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(4)
}
---next---
Key
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(1)
}
Value
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(5)
}
---next---
Key
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(2)
}
Value
array(2) {
  [1]=>
  int(3)
  ["second"]=>
  int(6)
}
---next---

Note that PHP 5.4 and 5.3 do not support accessing the key() values in foreach loops because they expect them to not be an array - doing so will cause "Warning: Illegal type returned from MultipleIterator::key()" and the result of (int)0 as the key for all iterations.

Without the MultipleIterator::MIT_KEYS_ASSOC flag, the MultipleIterator will create numeric indices based on the order of attachment.

3

= new ArrayIterator(array(1,2,3));
$it2 = new ArrayIterator(array(4,5,6));$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);$multipleIterator->attachIterator($it1, 1);
$multipleIterator->attachIterator($it2, 'second');

foreach (

$multipleIterator as $key => $value) {
    echo
"Key\n"; var_dump($key);
    echo
"Value\n"; var_dump($value);
    echo
"---next---\n";
}
?>

Result with PHP 5.5.0 and up:

Key
array(2) {
  [1]=>
  int(0)
  ["second"]=>
  int(0)
}
Value
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(4)
}
---next---
Key
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(1)
}
Value
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(5)
}
---next---
Key
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(2)
}
Value
array(2) {
  [1]=>
  int(3)
  ["second"]=>
  int(6)
}
---next---

Note that PHP 5.4 and 5.3 do not support accessing the key() values in foreach loops because they expect them to not be an array - doing so will cause "Warning: Illegal type returned from MultipleIterator::key()" and the result of (int)0 as the key for all iterations.

Without the MultipleIterator::MIT_KEYS_ASSOC flag, the MultipleIterator will create numeric indices based on the order of attachment.

4

valid() will be valid if any or all iterators are valid, depending on the setting of the $flags - with ANY, you can iterate over a set of iterators with some of them ending before others, and get NULL results from these iterators until the last iterator is at it's end. With ALL, iteration stops when the first iterator stops delivering results.2

13 năm trước

Robert_e_lee tại Dell Dot Com ¶

= new ArrayIterator(array(1,2,3));
$it2 = new ArrayIterator(array(4,5,6));$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);$multipleIterator->attachIterator($it1, 1);
$multipleIterator->attachIterator($it2, 'second');

foreach (

$multipleIterator as $key => $value) {
    echo
"Key\n"; var_dump($key);
    echo
"Value\n"; var_dump($value);
    echo
"---next---\n";
}
?>

Result with PHP 5.5.0 and up:

Key
array(2) {
  [1]=>
  int(0)
  ["second"]=>
  int(0)
}
Value
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(4)
}
---next---
Key
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(1)
}
Value
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(5)
}
---next---
Key
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(2)
}
Value
array(2) {
  [1]=>
  int(3)
  ["second"]=>
  int(6)
}
---next---

Note that PHP 5.4 and 5.3 do not support accessing the key() values in foreach loops because they expect them to not be an array - doing so will cause "Warning: Illegal type returned from MultipleIterator::key()" and the result of (int)0 as the key for all iterations.

Without the MultipleIterator::MIT_KEYS_ASSOC flag, the MultipleIterator will create numeric indices based on the order of attachment.

6

12 năm trước

Iterator :: tua lại - tua lại trình lặp lại phần tử đầu tiên

= new ArrayIterator(array(1,2,3));
$it2 = new ArrayIterator(array(4,5,6));$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);$multipleIterator->attachIterator($it1, 1);
$multipleIterator->attachIterator($it2, 'second');

foreach (

$multipleIterator as $key => $value) {
    echo
"Key\n"; var_dump($key);
    echo
"Value\n"; var_dump($value);
    echo
"---next---\n";
}
?>

Result with PHP 5.5.0 and up:

Key
array(2) {
  [1]=>
  int(0)
  ["second"]=>
  int(0)
}
Value
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(4)
}
---next---
Key
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(1)
}
Value
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(5)
}
---next---
Key
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(2)
}
Value
array(2) {
  [1]=>
  int(3)
  ["second"]=>
  int(6)
}
---next---

Note that PHP 5.4 and 5.3 do not support accessing the key() values in foreach loops because they expect them to not be an array - doing so will cause "Warning: Illegal type returned from MultipleIterator::key()" and the result of (int)0 as the key for all iterations.

Without the MultipleIterator::MIT_KEYS_ASSOC flag, the MultipleIterator will create numeric indices based on the order of attachment.

7

Iterator :: hợp lệ - kiểm tra xem vị trí hiện tại có hợp lệ không

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.1

= new ArrayIterator(array(1,2,3));
$it2 = new ArrayIterator(array(4,5,6));$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);$multipleIterator->attachIterator($it1, 1);
$multipleIterator->attachIterator($it2, 'second');

foreach (

$multipleIterator as $key => $value) {
    echo
"Key\n"; var_dump($key);
    echo
"Value\n"; var_dump($value);
    echo
"---next---\n";
}
?>

Result with PHP 5.5.0 and up:

Key
array(2) {
  [1]=>
  int(0)
  ["second"]=>
  int(0)
}
Value
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(4)
}
---next---
Key
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(1)
}
Value
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(5)
}
---next---
Key
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(2)
}
Value
array(2) {
  [1]=>
  int(3)
  ["second"]=>
  int(6)
}
---next---

Note that PHP 5.4 and 5.3 do not support accessing the key() values in foreach loops because they expect them to not be an array - doing so will cause "Warning: Illegal type returned from MultipleIterator::key()" and the result of (int)0 as the key for all iterations.

Without the MultipleIterator::MIT_KEYS_ASSOC flag, the MultipleIterator will create numeric indices based on the order of attachment.

8

= new ArrayIterator(array(1,2,3));
$it2 = new ArrayIterator(array(4,5,6));$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);$multipleIterator->attachIterator($it1, 1);
$multipleIterator->attachIterator($it2, 'second');

foreach (

$multipleIterator as $key => $value) {
    echo
"Key\n"; var_dump($key);
    echo
"Value\n"; var_dump($value);
    echo
"---next---\n";
}
?>

Result with PHP 5.5.0 and up:

Key
array(2) {
  [1]=>
  int(0)
  ["second"]=>
  int(0)
}
Value
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(4)
}
---next---
Key
array(2) {
  [1]=>
  int(1)
  ["second"]=>
  int(1)
}
Value
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(5)
}
---next---
Key
array(2) {
  [1]=>
  int(2)
  ["second"]=>
  int(2)
}
Value
array(2) {
  [1]=>
  int(3)
  ["second"]=>
  int(6)
}
---next---

Note that PHP 5.4 and 5.3 do not support accessing the key() values in foreach loops because they expect them to not be an array - doing so will cause "Warning: Illegal type returned from MultipleIterator::key()" and the result of (int)0 as the key for all iterations.

Without the MultipleIterator::MIT_KEYS_ASSOC flag, the MultipleIterator will create numeric indices based on the order of attachment.

9

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

Rocketinabog tại Techno-Monks Dot Net

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.1

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

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

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",
    );  
7

Shaun tại slickdesign dot com dot au ¶

3 năm trước

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

This iterator has a misleading name and description - it actually acts as a parallel iterator: You attach one or more iterators with a key, integer or NULL, and when you iterate over the MultipleIterator, as the result for current() you get ALL results from all attached iterators as an array (under the key or integer you attached it with), and the same is true for the key() call.7

    public function2

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 ..

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.. These can be used within loops instead of scalar arrays. Iterables are commonly used as object collections.

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. Ví dụ #3 Ví dụ về loại trả về. function Bar (): itable {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. Example #3 Iterable return type example. function bar(): iterable {

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.