递归函数返回NULL但我可以回显或var_dump()

时间:2018-05-07 17:32:16

标签: php recursion

我有一个嵌套菜单(侧边栏),如下所示:

$menu = [
    'dashboard' => [
        'type' => 'item',
        'name' => 'bacheca',
        'url' => 'home.php',
    ],
    'administration' => [
        'type' => 'header',
        'name' => 'amministrazione',
        'items' => [
            'configuration' => [
                'type' => 'item',
                'name' => 'configurazione',
                'url' => 'configuration.php',
            ],
            'admins' => [
                'type' => 'item',
                'name' => 'amministratori',
                'url' => 'admins.php',
            ],
            'activities' => [
                'type' => 'item',
                'name' => 'attività',
                'url' => 'activities.php',
            ],
        ],
    ],
    'intranet' => [
        'type' => 'header',
        'name' => 'intranet',
        'items' => [
            'chiefs' => [
                'type' => 'item',
                'name' => 'capiarea',
                'url' => 'chiefs.php',
            ],
            'agents' => [
                'type' => 'item',
                'name' => 'informatori',
                'url' => 'agents.php',
            ],
            'samples' => [
                'type' => 'tree',
                'name' => 'campioni',
                'url' => '#',
                'submenu' => array_merge(
                    [
                        'dummy' => [
                            'type' => 'item',
                            'name' => 'dummy',
                            'url' => 'dummy.php',
                        ],
                    ],
                    [
                        'temperature' => [
                            'type' => 'item',
                            'name' => 'temperature',
                            'url' => 'temperature.php',
                        ],
                    ]),
            ],
            'files' => [
                'type' => 'item',
                'name' => 'riferimenti normativi',
                'url' => 'files.php',
            ],
            'documents' => [
                'type' => 'item',
                'name' => 'documenti',
                'url' => 'documents.php',
            ],
        ],
    ],
];

使用递归函数,我尝试找到匹配的'项目'在菜单中输入并将其返回,以供将来详细说明(类型'标题''树'不是目标)。这是我写的递归函数:

function item($needle, $haystack, $result = []){
    foreach ($haystack as $key => $value){
        switch ($value['type']) {
            case 'header':
                if (isset($value['items'])){
                    item($needle, $value['items'], $result);
                }
                break;
            case 'tree':
                if (isset($value['submenu'])){
                    item($needle, $value['submenu'], $result);
                }
                break;
            default:
                if ($needle == $key){
                    $result['name'] = $value['name'];
                    $result['url'] = $value['url'];
                    return $result;
                }
        }
    }
}

我不知道为什么,这个函数什么都不返回:如果我输入var_dump(item('chiefs'));我希望它用chief键返回该项,但我什么都没得到

问题在于return声明:在if ($needle == $key){ .. }条件下我可以echovar_dump()匹配数组,但是当我使用return $result时,它会打印NULL

这是一个"live" script to play with:我哪里错了?

1 个答案:

答案 0 :(得分:3)

https://www.tehplayground.com/Ix0ODne19Acsjvyx

问题在于

case 'header':
        if (isset($value['items'])){
            item($needle, $value['items'], $result);
        }
        break;
case 'tree':
        if (isset($value['submenu'])){
            item($needle, $value['submenu'], $result);
        }
        break;

如果它进入其中任何一个并找到你要找的东西,你就不会返回任何东西。

你需要保存递归的返回值,如果它不为null,则返回它,否则,继续。

case 'header':
        if (isset($value['items'])){
            $v = item($needle, $value['items'], $result);
            if ($v) return $v;
        }
        break;
case 'tree':
        if (isset($value['submenu'])){
            $v = item($needle, $value['submenu'], $result);
            if ($v) return $v;
        }
        break;