[PHP-NOTES] note 130291 added to recursiveiteratoriterator.getsubiterator

[email protected] ("[email protected]")
Newsgroups php.notes
Message-ID <[email protected]>
$testArray = [
    'a' => [
        'b' => [
            'c' => 1
        ],
        'd' => [
            'e' => [
                'f' => 2
            ]
        ]
    ]
];

$modes = [
    'LEAVES_ONLY' => \RecursiveIteratorIterator::LEAVES_ONLY,
    'SELF_FIRST' => \RecursiveIteratorIterator::SELF_FIRST,
    'CHILD_FIRST' => \RecursiveIteratorIterator::CHILD_FIRST
];

function logIteratorTraversal(\RecursiveIteratorIterator $iterator, string $modeName)
{
    $maxDepth = 0;
    $log = [];

    foreach ($iterator as $key => $value) {
        $currentDepth = $iterator->getDepth();
        $maxDepth = max($maxDepth, $currentDepth);

        // 构建路径
        $path = [];
        for ($i = 0; $i <= $currentDepth; $i++) {
            $sub = $iterator->getSubIterator($i);
            $path[] = $sub->key();
        }

        $log[] = sprintf(
            "模式: %-11s | 当前深度: %d | 路径: %-16s | 类型: %s",
            $modeName,
            $currentDepth,
            implode('→', $path),
            is_array($value) ? '非叶子节点' : '叶子节点'
        );
    }

    echo "=== {$modeName} 模式遍历日志 ===\n";
    echo implode("\n", $log);
    echo "\n最大实际深度:" . ($maxDepth + 1) . "\n\n";
}

foreach ($modes as $name => $mode) {
    $iterator = new \RecursiveIteratorIterator(
        new \RecursiveArrayIterator($testArray),
        $mode,
        \RecursiveIteratorIterator::CATCH_GET_CHILD
    );
    logIteratorTraversal($iterator, $name);
}

=== LEAVES_ONLY 模式遍历日志 ===
模式: LEAVES_ONLY | 当前深度: 2 | 路径: a→b→c        | 类型: 叶子节点
模式: LEAVES_ONLY | 当前深度: 3 | 路径: a→d→e→f    | 类型: 叶子节点
最大实际深度:4

=== SELF_FIRST 模式遍历日志 ===
模式: SELF_FIRST  | 当前深度: 0 | 路径: a                | 类型: 非叶子节点
模式: SELF_FIRST  | 当前深度: 1 | 路径: a→b            | 类型: 非叶子节点
模式: SELF_FIRST  | 当前深度: 2 | 路径: a→b→c        | 类型: 叶子节点
模式: SELF_FIRST  | 当前深度: 1 | 路径: a→d            | 类型: 非叶子节点
模式: SELF_FIRST  | 当前深度: 2 | 路径: a→d→e        | 类型: 非叶子节点
模式: SELF_FIRST  | 当前深度: 3 | 路径: a→d→e→f    | 类型: 叶子节点
最大实际深度:4

=== CHILD_FIRST 模式遍历日志 ===
模式: CHILD_FIRST | 当前深度: 2 | 路径: a→b→c        | 类型: 叶子节点
模式: CHILD_FIRST | 当前深度: 1 | 路径: a→b            | 类型: 非叶子节点
模式: CHILD_FIRST | 当前深度: 3 | 路径: a→d→e→f    | 类型: 叶子节点
模式: CHILD_FIRST | 当前深度: 2 | 路径: a→d→e        | 类型: 非叶子节点
模式: CHILD_FIRST | 当前深度: 1 | 路径: a→d            | 类型: 非叶子节点
模式: CHILD_FIRST | 当前深度: 0 | 路径: a                | 类型: 非叶子节点
最大实际深度:4
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 39.166.18.151)
----
Manual Page -- https://php.net/manual/en/recursiveiteratoriterator.getsubiterator.php
Edit        -- https://main.php.net/note/edit/130291
Del: integrated  -- https://main.php.net/note/delete/130291/integrated
Del: useless     -- https://main.php.net/note/delete/130291/useless
Del: bad code    -- https://main.php.net/note/delete/130291/bad+code
Del: spam        -- https://main.php.net/note/delete/130291/spam
Del: non-english -- https://main.php.net/note/delete/130291/non-english
Del: in docs     -- https://main.php.net/note/delete/130291/in+docs
Del: other reasons-- https://main.php.net/note/delete/130291
Reject      -- https://main.php.net/note/reject/130291
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.