라이브러리

[PHP] ArrayIterator::__construct - ArrayIterator 구성




PHP에서 ArrayIterator는 SplIterator의 하위 클래스로, 배열을 반복할 수 있는 Iterator를 제공합니다. ArrayIterator는 생성자(__construct) 메서드를 통해 초기화할 수 있습니다.

ArrayIterator::__construct


ArrayIterator::__construct 메서드는 ArrayIterator 객체를 생성하고, 배열을 반복할 수 있도록 초기화합니다.

# 생성자 매개변수


ArrayIterator::__construct 메서드는 다음과 같은 매개변수를 받습니다.

- `$array`: 반복할 배열

# 예제


#hostingforum.kr
php

// 예제 1: 기본적인 ArrayIterator 사용

$array = array(1, 2, 3, 4, 5);

$iterator = new ArrayIterator($array);



while ($iterator->valid()) {

    echo $iterator->current() . "
";

    $iterator->next();

}



# 예제 2: ArrayIterator 사용하여 배열의 키와 값을 함께 반복


#hostingforum.kr
php

// 예제 2: ArrayIterator 사용하여 배열의 키와 값을 함께 반복

$array = array(

    'apple' => '사과',

    'banana' => '바나나',

    'orange' => '오レン지'

);

$iterator = new ArrayIterator($array);



while ($iterator->valid()) {

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

    $iterator->next();

}



# 예제 3: ArrayIterator 사용하여 배열의 키와 값을 함께 반복 (key => value)


#hostingforum.kr
php

// 예제 3: ArrayIterator 사용하여 배열의 키와 값을 함께 반복 (key => value)

$array = array(

    'apple' => '사과',

    'banana' => '바나나',

    'orange' => '오렌지'

);

$iterator = new ArrayIterator($array);



while ($iterator->valid()) {

    echo $iterator->key() . " => " . $iterator->current() . "
";

    $iterator->next();

}



# 예제 4: ArrayIterator 사용하여 배열의 키와 값을 함께 반복 (key => value) - foreach 사용


#hostingforum.kr
php

// 예제 4: ArrayIterator 사용하여 배열의 키와 값을 함께 반복 (key => value) - foreach 사용

$array = array(

    'apple' => '사과',

    'banana' => '바나나',

    'orange' => '오렌지'

);

$iterator = new ArrayIterator($array);



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

    echo $key . " => " . $value . "
";

}



ArrayIterator는 배열을 반복할 수 있는 Iterator를 제공하며, 생성자 메서드를 통해 초기화할 수 있습니다. ArrayIterator는 반복할 배열을 매개변수로 받으며, 반복할 배열을 초기화합니다. ArrayIterator를 사용하여 배열의 키와 값을 함께 반복할 수 있으며, foreach 문을 사용하여 반복할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 77건 / 1 페이지

검색

게시물 검색