dtkwt62022
2018-07-28 11:24
浏览 64
已采纳

PHP如何将单个数组转换为多维数组?

I have that single array and I need to convert in a multidimensional array without using array_merge, array_replace_recurcive etc, just an autonomous function:

$single = [
    0 => 'one',
    1 => 'two',
    2 => 'tree',
    3 => 'four',
    4 => 'five'
];

And convert to look like this, with the last key as value:

$multidimentional = [
    'one' => [
        'two' => [
            'tree' => [
                'four' => 'five'
            ]
        ]
    ]
];

I have create a recursion function if this helps:

function array_replace_recursive($defaults, $replaces) {

    if(is_null($replaces)) {
        $replaces = [];
    }

    if(!is_array($defaults) || !is_array($replaces)) {
        return $replaces;
    }

    foreach($defaults as $key => $value) {
        if(!array_key_exists($key, $replaces) || is_null($replaces[$key])) {
            $replaces[$key] = $value;
        } else {
            if(is_array($replaces[$key]) && is_array($value)) {
                $replaces[$key] = array_replace_recursive($replaces[$key], $value);
            }
        }
    }

    return $replaces; 
}

图片转代码服务由CSDN问答提供 功能建议

我有单个数组,我需要在不使用 array_merge 的情况下在多维数组中进行转换 , array_replace_recurcive 等,只是一个自治函数:

  $ single = [
 0 0 =>  'one',
 1 =>  'two',
 2 =>  'tree',
 3 =>  '四',
 4 =>  'five'
]; 
   
 
 

转换为如下所示,最后一个键为值:

 < 代码> $ multidimentional = [
'one'=&gt;  [
'两个'=&gt;  [
'tree'=&gt;  [
'四'=&gt;  '五'
] 
] 
] 
]; 
   
 
 

如果有帮助,我创建了一个递归函数: \ n

  function array_replace_recursive($ defaults,$ replaces){
 
 if if(is_null($ replaceces)){
 $ replaceces = []; 
} 
 
 if(!is_array(  $ defaults)||!is_array($ replaceces)){
 return $ replaces; 
} 
 
 foreach($ defaults as $ key =&gt; $ value){
 if(!array_key_exists($ key,  $替换)|| is_null($替换[$ key])){
 $替换[$ key] = $ value; 
} else {
 if(is_array($ replaceces [$ key])&amp;&amp;  is_array($ value)){
 $替换[$ key] = array_replace_recursive($替换[$ key],$ value); 
} 
} 
} 
 
返回$ replaceces;  
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doukong9982 2018-07-28 11:53
    已采纳

    Thinking in recursion, you can write a base case that returns the value of the currently seen item if it is one less than the length of the array.

    $singleDim = [
        0 => 'one',
        1 => 'two',
        2 => 'tree',
        3 => 'four',
        4 => 'five'
    ];
    
    function toMultiDimArray($arr, $seen=0) {
        if ([] === $arr) {
            return [];
        }
    
        if(count($arr) - 1 === $seen) {
            return $arr[$seen];
        }
    
        return [
           $arr[$seen] => toMultiDimArray($arr, $seen+1)
        ];
    }
    
    $multiDim = toMultiDimArray($singleDim);
    
    var_dump($multiDim);
    
    array(1) {
      ["one"]=>
      array(1) {
        ["two"]=>
        array(1) {
          ["tree"]=>
          array(1) {
            ["four"]=>
            string(4) "five"
          }
        }
      }
    }
    
    点赞 打赏 评论
  • douke8473 2018-07-28 11:35

    There's nothing recursive about this. It's just iterating over the $single array backwards and then embedding the result inside the new key each time:

    function foo( $single ) {
        $multidimensional = [ $single[ count($single) - 1 ] ];
    
        for( $i = count($single) - 2; $i >= 0; $i--  ) {
          $multidimensional = [ $single[$i] => $multidimensional ];
        }
    }
    
    点赞 打赏 评论
  • doujiyong7604 2018-07-28 12:35

    You can do this with PHP (as of 5.6) argument unpacking and a very simple recursive function:

    $single = [
        0 => 'one',
        1 => 'two',
        2 => 'tree',
        3 => 'four',
        4 => 'five'
    ];
    
    function convert($key, ...$values) {
        if (count($values) == 1)
            return array($key => $values[0]);
        else
            return array($key => convert(...$values));
    }
    
    print_r(convert(...$single));
    

    Output:

    Array
    (
        [one] => Array
            (
                [two] => Array
                    (
                        [tree] => Array
                            (
                                [four] => five
                            )
                    )
            )
    )
    

    You can also do it without using count (only isset):

    function convert2($key, ...$values) {
        if (!isset($values[1]))
            return array($key => $values[0]);
        else
            return array($key => convert(...$values));
    }
    
    print_r(convert2(...$single));
    

    Output:

    Array
    (
        [one] => Array
            (
                [two] => Array
                    (
                        [tree] => Array
                            (
                                [four] => five
                            )
                    )
            )
    )
    
    点赞 打赏 评论
  • dongnaizao8039 2018-07-28 12:48
    $single = [
        0 => 'one',
        1 => 'two',
        2 => 'tree',
        3 => 'four',
        4 => 'five'
    ];
    
    function to_multidimentional($array, $count = 0, $seen = 0) {
    
        if($count - 1 === $seen) {
            return $array[$seen];
        }
    
        return [
           $array[$seen] => to_multidimentional($array, $count, $seen + 1)
        ];
    }
    
    $single = to_multidimentional($single, count($single));
    
    print_r($single);
    
    exit;
    
    点赞 打赏 评论

相关推荐 更多相似问题