[ create a new paste ] login | about

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

PHP, pasted on Apr 5:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

$a = array(
    'Alpha' => array('Red' => 'one', 'Blue' => 'two'),
    'Bravo' => array('Blue' => 'three'));

function convert_array($from){
    if(!is_array($from)){
        return false;
    }
    $to = array();
    foreach($from as $k=>$v){
        $to[] = array(
            'title' => $k,
            'children' => convert_array($v)
        );
    }
    return $to;
}

print_r(convert_array($a));


Output:
Array
(
    [0] => Array
        (
            [title] => Alpha
            [children] => Array
                (
                    [0] => Array
                        (
                            [title] => Red
                            [children] => 
                        )

                    [1] => Array
                        (
                            [title] => Blue
                            [children] => 
                        )

                )

        )

    [1] => Array
        (
            [title] => Bravo
            [children] => Array
                (
                    [0] => Array
                        (
                            [title] => Blue
                            [children] => 
                        )

                )

        )

)


Create a new paste based on this one


Comments: