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] => 
    )
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题