doufu1939 2018-11-16 19:04
浏览 23
已采纳

用递归分隔带有分隔符的多向数组键

I am trying to convert a multi-directional to an array of keys with delimiter with a recursive php function. Here is what i have so far. Not sure why its returning partial array.

$array = [
        'arr' => 1,
        'arr2' => [
            '_arr2' => 'test',
            '_arr2_2' => 'test 2',
            '_arr2_3' => 3,
            '_arr2_4' => [
                '__arr2' => 'tt',
                '__arr2_2' => 'ttww',
                'sub-sub-field' => [
                    'ww' => 1
                ]
            ],
            '_arr2_5' => [
                '__arr2_5' => 'some'
            ]
        ],
        'arr3' => 'test',
        'arr4' => null,
        'arr5' => [
            '_arr5' => [
                '___arr5' => 1
            ],
            '_arr5_1' => null
        ],
    ];

function print_array_reccur ($array, $str = '') 
{
    if (empty($array)) return null;

    $arr = [];

    foreach ($array as $key => $val) 
    {
        if (is_array($val)) 
        {
            if ($str == '') 
            {
                $arr = print_array_reccur($val, $key);
            } 
            else 
            {
                $arr = print_array_reccur($val, $str . '/' . $key);
            }
        } 
        else 
        {
            if ($str == '') 
            {
                $arr[$key] = $val;
            } 
            else 
            {
                $arr[$str . '/' . $key] = $val;
            }
        }
    }

    return $arr;
}

currently i am getting the below partial array output. Any idea as to why this is happening?

   Array
(
    [arr5/_arr5/___arr5] => 1
    [arr5/_arr5_1] => 
)
  • 写回答

1条回答 默认 最新

  • duandie5707 2018-11-16 19:18
    关注

    From what I can tell (and with the help of your test case - which always helps :), the problem is that when you were returning the new values from the sub array processing, this was overwriting the existing contents. I've commented the two lines but just that I've used += to add the new data to the existing array data...

    function print_array_reccur ($array, $str = '')
    {
        if (empty($array)) return null;
    
        $arr = [];
    
        foreach ($array as $key => $val)
        {
            if (is_array($val))
            {
                if ($str == '')
                {
                    // Add new data into the existing array using +=
                    $arr += print_array_reccur($val, $key);
                }
                else
                {
                    // Add new data into the existing array using +=
                    $arr += print_array_reccur($val, $str . '/' . $key);
                }
            }
            else
            {
                if ($str == '')
                {
                    $arr[$key] = $val;
                }
                else
                {
                    $arr[$str . '/' . $key] = $val;
                }
            }
        }
    
        return $arr;
    }
    

    Hopefully the return value is what your after...

    Array
    (
        [arr] => 1
        [arr2/_arr2] => test
        [arr2/_arr2_2] => test 2
        [arr2/_arr2_3] => 3
        [arr2/_arr2_4/__arr2] => tt
        [arr2/_arr2_4/__arr2_2] => ttww
        [arr2/_arr2_4/sub-sub-field/ww] => 1
        [arr2/_arr2_5/__arr2_5] => some
        [arr3] => test
        [arr4] => 
        [arr5/_arr5/___arr5] => 1
        [arr5/_arr5_1] => 
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,