라이브러리

[PHP] MultipleIterator::key - 등록된 반복자 인스턴스를 가져옵니다.




MultipleIterator::key


PHP의 `MultipleIterator` 클래스는 여러 배열을 동시에 반복할 수 있는 기능을 제공합니다. `MultipleIterator::key` 메서드는 반복 중에 현재 반복되는 배열의 키를 반환합니다.

사용법


`MultipleIterator::key` 메서드는 반복 중에 호출할 수 있습니다. 이 메서드는 현재 반복되는 배열의 키를 반환합니다. 예를 들어, 다음과 같이 사용할 수 있습니다.

#hostingforum.kr
php

$iterator = new MultipleIterator();

$iterator->attachIterator(new ArrayIterator([1, 2, 3]));

$iterator->attachIterator(new ArrayIterator(['a', 'b', 'c']));

$iterator->attachIterator(new ArrayIterator([true, false, true]));



foreach ($iterator as $key => $value) {

    echo "Key: $key, Value: $value
";

    echo "Current Key: " . $iterator->key() . "
";

}



예제


이 예제에서는 `MultipleIterator` 클래스를 사용하여 3개의 배열을 동시에 반복합니다. 각 반복에서 `MultipleIterator::key` 메서드를 호출하여 현재 반복되는 배열의 키를 출력합니다.

#hostingforum.kr
php

$iterator = new MultipleIterator();

$iterator->attachIterator(new ArrayIterator([1, 2, 3]));

$iterator->attachIterator(new ArrayIterator(['a', 'b', 'c']));

$iterator->attachIterator(new ArrayIterator([true, false, true]));



foreach ($iterator as $key => $value) {

    echo "Key: $key, Value: $value
";

    echo "Current Key: " . $iterator->key() . "
";

    echo "------------------------
";

}



결과


이 예제의 결과는 다음과 같습니다.

#hostingforum.kr


Key: 0, Value: 1

Current Key: 0

------------------------

Key: 1, Value: 2

Current Key: 1

------------------------

Key: 2, Value: 3

Current Key: 2

------------------------

Key: 0, Value: a

Current Key: 0

------------------------

Key: 1, Value: b

Current Key: 1

------------------------

Key: 2, Value: c

Current Key: 2

------------------------

Key: 0, Value: 1

Current Key: 0

------------------------

Key: 1, Value: 

Current Key: 1

------------------------

Key: 2, Value: 1

Current Key: 2

------------------------



결론


`MultipleIterator::key` 메서드는 반복 중에 현재 반복되는 배열의 키를 반환하는 기능을 제공합니다. 이 메서드는 반복 중에 호출할 수 있으며, 현재 반복되는 배열의 키를 반환합니다. 이 기능은 여러 배열을 동시에 반복할 때 유용합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 1 페이지

검색

게시물 검색