PHP 创建嵌套数组中每个值的面包屑列表 [英] PHP Create breadcrumb list of every value in nested array

查看:47
本文介绍了PHP 创建嵌套数组中每个值的面包屑列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数组:

<预><代码>['申请人' =>['用户' =>['用户名' =>真的,'密码' =>真的,'数据' =>['价值' =>真的,'anotherValue' =>真的]]]]

我想要做的是将该数组转换为如下所示的数组:

<预><代码>['申请人.用户.用户名','申请人.用户.密码','applicant.user.data.value','applicant.user.data.anotherValue']

基本上,我需要以某种方式循环遍历嵌套数组,每次到达叶节点时,将该节点的整个路径保存为点分隔字符串.

只有以 true 为值的键是叶节点,每隔一个节点将始终是一个数组.我将如何完成这项工作?

编辑

这是我迄今为止尝试过的,但没有给出预期的结果:

 $tree = $this->getTree();//返回上面嵌套的数组$crumbs = [];$recurse = function ($tree, &$currentTree = []) 使用 (&$recurse, &$crumbs){foreach ($tree as $branch => $value){如果 (is_array($value)){$currentTree[] = $branch;$recurse($value, $currentTree);}别的{$crumbs[] = implode('.', $currentTree);}}};$递归($树);

解决方案

这个函数做你想做的:

function flattenArray($arr) {$输出= [];foreach ($arr as $key => $value) {如果 (is_array($value)) {foreach(flattenArray($value) as $flattenKey => $flattenValue) {$output["${key}.${flattenKey}"] = $flattenValue;}} 别的 {$output[$key] = $value;}}返回 $output;}

您可以在此处看到它运行.

I have an array that looks like the following:

[
    'applicant' => [
        'user' => [
            'username' => true,
            'password' => true,
            'data' => [
                'value' => true,
                'anotherValue' => true
            ]
        ]
    ]
]

What I want to be able to do is convert that array into an array that looks like:

[
    'applicant.user.username',
    'applicant.user.password',
    'applicant.user.data.value',
    'applicant.user.data.anotherValue'
]

Basically, I need to somehow loop through the nested array and every time a leaf node is reached, save the entire path to that node as a dot separated string.

Only keys with true as a value are leaf nodes, every other node will always be an array. How would I go about accomplishing this?

edit

This is what I have tried so far, but doesnt give the intended results:

    $tree = $this->getTree(); // Returns the above nested array
    $crumbs = [];

    $recurse = function ($tree, &$currentTree = []) use (&$recurse, &$crumbs)
    {
        foreach ($tree as $branch => $value)
        {
            if (is_array($value))
            {
                $currentTree[] = $branch;
                $recurse($value, $currentTree);
            }
            else
            {
                $crumbs[] = implode('.', $currentTree);
            }
        }
    };

    $recurse($tree);

解决方案

This function does what you want:

function flattenArray($arr) {
    $output = [];

    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            foreach(flattenArray($value) as $flattenKey => $flattenValue) {
                $output["${key}.${flattenKey}"] = $flattenValue;
            }
        } else {
            $output[$key] = $value;
        }
    }

    return $output;
}

You can see it running here.

这篇关于PHP 创建嵌套数组中每个值的面包屑列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆