php create multidimensional array from flat one

后端 未结 5 2032
时光说笑
时光说笑 2020-12-18 11:21

I have an array like this:

 \'foo\', 1 => \'bar\', ..., x => \'foobar\' );
?>

What is the fas

5条回答
  •  死守一世寂寞
    2020-12-18 12:02

    You can use a recursive function so that you're not iterating through the array each step. Here's such a function I wrote.

    function expand_arr($arr)
    {
        if (empty($arr))
            return array();
    
        return array(
            array_shift($arr) => expand_arr($arr)
        );
    }
    

    Your question is a little unclear since in your initial statement you're using the next value in the array as the next step down's key and then at the end of your example you're using the original key as the only key in the next step's key.

提交回复
热议问题