eval.in

Paste #18420

PHP — PHP 5.4.6, pasted 11 months ago

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/**
 * PHP - Nested array keys based on string lines
 * @link http://stackoverflow.com/a/16305236/2261774
 */
$file = 'data://text/plain;base64,' . base64_encode('Brazil|SaoPaulo|Diadema|RuadaFe Brazil|SaoPaulo|Diadema|RuadoLimoeiro Brazil|SaoPaulo|SaoCaetano|RuadasLaranjeiras Brazil|Parana|Curitiba|ComendadorAraujo USA|NewJersey|JerseyCity|WhashingtonBoulervard USA|NewJersey|JerseyCity|RiverCourt');

$separator = [' ', '|'];

$buffer = file_get_contents($file);
$entries = explode($separator[0], $buffer);
$result = [];
$named[] = &$result;
foreach ($entries as $entry) {
    $each     = explode($separator[1], $entry);
    $pointer  = & $result;
    while ($current = array_shift($each)) {
        if (!isset($pointer[$current])) {
            unset($children);
            $children = [];
            $named[] = &$children;
            $pointer[$current] = ['name' => $current, 'children' => &$children];
        }
        $pointer = & $pointer[$current]['children'];
    }
}

foreach($named as $offset => $namedArray) {
    $keys = array_keys($namedArray);
    foreach($keys as $key) {
        $named[$offset][] = &$namedArray[$key];
        unset($named[$offset][$key]);
    }
}

print_r($result);

Program Output

Array
(
    [0] => Array
        (
            [name] => Brazil
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => SaoPaulo
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => Diadema
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [name] => RuadaFe
                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [name] => RuadoLimoeiro
                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [name] => SaoCaetano
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [name] => RuadasLaranjeiras
                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [name] => Parana
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => Curitiba
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [name] => ComendadorAraujo
                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [name] => USA
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => NewJersey
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => JerseyCity
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [name] => WhashingtonBoulervard
                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [name] => RiverCourt
                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

OK (0.028 sec real, 0.054 sec wall, 13 MB, 4717 syscalls)

Fork