Home >Backend Development >PHP Tutorial >请问php可以将一维转成多维吗?

请问php可以将一维转成多维吗?

WBOY
WBOYOriginal
2016-06-06 20:14:311219browse

如:
$a=['a','b','c'];
希望得到结果

<code>var_dump($new)
'a'=>'b'=>'c'</code>

谢谢.

回复内容:

如:
$a=['a','b','c'];
希望得到结果

<code>var_dump($new)
'a'=>'b'=>'c'</code>

谢谢.

<code class="php">$a=['a','b','c','d','e','f','g'];
$last=null;
while($last=array_pop($a)) {
  if( null != $last ) array_push($a,[array_pop($a)=>$last]);
  if(count($a) </code>

Array
(

<code>[a] => Array
    (
        [b] => Array
            (
                [c] => Array
                    (
                        [d] => Array
                            (
                                [e] => Array
                                    (
                                        [f] => g
                                    )

                            )

                    )

            )

    )
</code>

)

我觉得不可以。

我建议写个递归函数,利用下标。递归特性太明显了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn