
ArrayIterator::__construct 메소드는 ArrayIterator 클래스의 생성자로, 객체를 초기화하는 역할을 합니다.
이 메소드는 다음과 같은 파라미터를 받을 수 있습니다.
- array: 반복할 배열을 전달합니다.
- string: 키를 사용하여 반복할 수 있습니다.
- int: 시작 인덱스를 지정합니다.
- int: 끝 인덱스를 지정합니다.
예를 들어, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$array = ['apple', 'banana', 'cherry'];
$iterator = new ArrayIterator($array);
foreach ($iterator as $value) {
echo $value . "n";
}
또는 키를 사용하여 반복할 수 있습니다.
#hostingforum.kr
php
$array = ['apple' => 1, 'banana' => 2, 'cherry' => 3];
$iterator = new ArrayIterator($array);
foreach ($iterator as $key => $value) {
echo "$key: $valuen";
}
또한, 시작 인덱스와 끝 인덱스를 지정할 수 있습니다.
#hostingforum.kr
php
$array = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
$iterator = new ArrayIterator($array, 1, 3);
foreach ($iterator as $value) {
echo $value . "n";
}
이러한 예제를 통해 ArrayIterator::__construct 메소드의 사용법을 이해할 수 있습니다.
2025-07-07 06:18