[ create a new paste ] login | about

Link: http://codepad.org/awXfJqCl    [ raw code | output | fork ]

PHP, pasted on Oct 3:
<?php

$flat_list = $tree = array();

$tree = array(
	"one",
	array(
		"two",
		"three",
		array(
			"four"
		),
		"five"
	),
	"six",
	"seven",
	array(
		"eight"
	)
);

function descend($into) {
	global $flat_list;
	if (is_array($into)) {
		foreach($into as $item) {
			descend($item);
		}
	} else {
		$flat_list[] = $into;
	}
}

descend($tree);

print_r($tree);
echo "\r\n";
print_r($flat_list);

?>


Output:
Array
(
    [0] => one
    [1] => Array
        (
            [0] => two
            [1] => three
            [2] => Array
                (
                    [0] => four
                )

            [3] => five
        )

    [2] => six
    [3] => seven
    [4] => Array
        (
            [0] => eight
        )

)

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
    [5] => six
    [6] => seven
    [7] => eight
)


Create a new paste based on this one


Comments: