从点注释创建树结构

时间:2017-11-07 09:06:24

标签: php

我第二天将点到树的结构转换成了结构。 有人可以帮忙吗?

    $input = [
        'inbox'               => ['name' => 'Inbox'],
        'inbox.first'         => ['name' => 'First'],
        'inbox.second'        => ['name' => 'Second'],
        'inbox.second.other'  => ['name' => 'Second Other'],
        'inbox.third.another' => ['name' => 'Third Another'],
    ];

    $expectedOutput = [
        'inbox' => [
            'name'       => 'Inbox',
            'mailbox'    => 'inbox',
            'subfolders' => [
                'first'  => [
                    'name'       => 'First',
                    'mailbox'    => 'inbox.first',
                    'subfolders' => [],
                ],
                'second' => [
                    'name'       => 'Second',
                    'mailbox'    => 'inbox.second',
                    'subfolders' => [
                        'other' => [
                            'name'       => 'Second Other',
                            'subfolders' => [],
                            'mailbox'    => 'inbox.second.other',
                        ],
                    ],
                ],
                'third'  => [
                    'subfolders' => [
                        'another' => [
                            'name'       => 'Third Another',
                            'subfolders' => [],
                            'mailbox'    => 'inbox.third.another',
                        ],
                    ],
                ],
            ],
        ],
    ];

1 个答案:

答案 0 :(得分:1)

您可以检查laravel函数Arr::set作为基础。如果您将数组的格式与该函数的输出匹配,则可以像下面那样使用它:

 //This is the set function from https://github.com/laravel/framework/blob/5.5/src/Illuminate/Support/Arr.php#L510
function set(&$array, $key, $value)
{
    if (is_null($key)) {
        return $array = $value;
    }
    $keys = explode('.', $key);
    while (count($keys) > 1) {
        $key = array_shift($keys);
        // If the key doesn't exist at this depth, we will just create an empty array
        // to hold the next value, allowing us to create the arrays to hold final
        // values at the correct depth. Then we'll keep digging into the array.
        if (! isset($array[$key]) || ! is_array($array[$key])) {
            $array[$key] = [];
        }
        $array = &$array[$key];
    }
    $array[array_shift($keys)] = $value;
    return $array;
}


$input = [
        'inbox'               => ['name' => 'Inbox'],
        'inbox.first'         => ['name' => 'First'],
        'inbox.second'        => ['name' => 'Second'],
        'inbox.second.other'  => ['name' => 'Second Other'],
        'inbox.third.another' => ['name' => 'Third Another'],
];

$newKeys = array_map(function ($key) {
    $k = explode(".",$key);
    $newkey = [];
    foreach ($k as $segment) {
        $newkey[] = $segment;
        $newkey[] = "subfolders";
    }
    return implode(".",$newkey);
}, array_keys($input)); 

$input = array_combine($newKeys, array_map(function ($value,$key) {
    return array_merge($value, ["mailbox"=>$key]);
},$input,array_keys($input)));

$res = [];
array_walk($input, function ($value,$key) use (&$res) {
    set($res,$key,$value);
});

演示:http://sandbox.onlinephpfunctions.com/code/9717e7606f099f1352a559c447b5225dd0d74f6c