Linear to nested array

<?php
$a = array('a', 'b', 'c');
foreach (array_reverse($a) as $v) {
    $b = isset($b) ? array($v => $b) : array($v);
}
print_r($b);
?>

Output

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [0] => c
                )

        )

)

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.