
RecursiveTreeIterator 클래스의 beginChildren 메서드는 트리의 자식 노드에 접근하는 방법을 제공합니다. beginChildren 메서드는 현재 노드의 자식 노드를 반환하는 이터레이터를 반환합니다.
트리 구조의 데이터를 다음과 같이 정의한 경우, beginChildren 메서드는 다음과 같이 작동합니다.
#hostingforum.kr
php
$tree = array(
'id' => 1,
'parent_id' => 0,
'name' => '루트 노드',
'children' => array(
array(
'id' => 2,
'parent_id' => 1,
'name' => '자식 노드 1',
'children' => array(
array(
'id' => 3,
'parent_id' => 2,
'name' => '자식 노드 1-1',
),
),
),
array(
'id' => 4,
'parent_id' => 1,
'name' => '자식 노드 2',
'children' => array(
array(
'id' => 5,
'parent_id' => 4,
'name' => '자식 노드 2-1',
),
),
),
),
);
$iterator = new RecursiveTreeIterator($tree);
$childrenIterator = $iterator->getChildren();
// 루트 노드의 자식 노드에 접근
foreach ($childrenIterator as $child) {
echo $child['name'] . "n";
// 자식 노드의 자식 노드에 접근
$grandchildrenIterator = $child->getChildren();
foreach ($grandchildrenIterator as $grandchild) {
echo $grandchild['name'] . "n";
}
}
위 예제에서, beginChildren 메서드는 루트 노드의 자식 노드에 접근하는 이터레이터를 반환합니다. 루트 노드의 자식 노드에 접근한 후, 자식 노드의 자식 노드에 접근하는 이터레이터를 반환합니다.
2025-03-27 00:36