在php中从多维数组创建嵌套的父子数组

时间:2015-06-17 04:37:25

标签: php arrays algorithm multidimensional-array

我有这个数组

$array =  Array
    (
        [a] => Array
            (
                [0] => b
                [1] => h
            )

        [b] => c
        [c] => d
        [h] => m
    )

我需要将数组转换为如下所示

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [c] => Array
                        (
                            [d] => Array
                                (
                                )

                        )

                )

            [h] => Array
                (
                    [m] => Array
                        (
                        )

                )

        )

)

我已经向One Dimensional数组提出了这个问题。

我尝试了[Creating nested parent child array from one dimensional array in php,我得到了以下数组

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [a] => Array
                        (
                            [h] => Array
                                (
                                    [b] => Array
                                        (
                                            [c] => Array
                                                (
                                                    [d] => Array
                                                        (
                                                        )

                                                    [h] => Array
                                                        (
                                                            [m] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

如何检查多维数组中是否存在键,如果存在,则将子项添加到现有键。帮助解决问题。在此先感谢

1 个答案:

答案 0 :(得分:2)

<?php 

$array = array( 
   'a' => array(0=>'b',1=>'h'),
   'b' => 'c',
   'c' => 'd',
   'h' => 'm',
);

$newArray    = array();
$secondarray = array();
$part        = &$newArray;
$i=1;

foreach($array as $first => $second)
{
    if($i==1)
    {
        $firstone=$first;
    }
    else
    {
        if($i==count($array))
        {
            $newArray[$first] = array($second => array());
            $secondarray[$firstone]=$newArray;
        }
        else
        {
            $part = &$part[$first];
            $part[$second] = array();
         }
     }
     $i++;
}

echo '<pre>';print_r($secondarray);

输出

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [c] => Array
                        (
                            [d] => Array
                                (
                                )

                        )

                )

            [h] => Array
                (
                    [m] => Array
                        (
                        )

                )

        )

)