
ArrayIterator::current 메서드는 현재 반복 중인 요소의 복사본을 반환합니다.
이 메서드의 반환 값은 실제 요소 자체가 아닌 복사본이기 때문에, 이 메서드를 사용하여 요소를 변경하는 경우, 변경된 요소는 반영되지 않습니다.
예를 들어, 다음 코드를 실행했을 때, $array는 여전히 ['apple', 'banana', 'cherry']로 유지됩니다.
#hostingforum.kr
php
$array = ['apple', 'banana', 'cherry'];
$iterator = new ArrayIterator($array);
echo $iterator->current(); // 'apple'
$iterator->current() = 'grape'; // 변경된 요소는 반영되지 않습니다.
echo $array[0]; // 'apple'
ArrayIterator::current 메서드를 사용하여 요소를 변경하고 싶다면, 실제 요소를 변경해야 합니다.
#hostingforum.kr
php
$array = ['apple', 'banana', 'cherry'];
$iterator = new ArrayIterator($array);
echo $iterator->current(); // 'apple'
$array[$iterator->key()] = 'grape'; // 실제 요소를 변경합니다.
echo $array[0]; // 'grape'
2025-08-10 14:59