<?php

$tree = [
    1,
    2,
    [
        3,
        3
    ],
    [
        4,
        4, [
            41,
            42
        ]
    ],
    5
];

$notVisited = $tree;

while ($notVisited) {
    $currentElement = array_shift($notVisited);
    if (is_array($currentElement)) {
        $notVisited = array_merge($currentElement, $notVisited);
    } else {
        var_dump($currentElement);
    }
}