创建分层多维和关联数组

时间:2013-01-25 09:30:41

标签: php arrays multidimensional-array hierarchical-data dynamic-arrays

我需要从“平面”数组创建一个巨大的多维和关联数组,以使其符合json:

$data = array (
    'Europe',
    'West',
    'Germany',
    'France',
    'Switzerland',
    'East',
    'Czech Republic',
    'Slovakia',
    'Poland')

对于每个实体,我能够说明它是否合并(欧洲,西方和东方),或基本要素(即没有子女)

我想要实现的是以下数组:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [entity] => Europe
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [entity] => West
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [entity] => France
                                                )
                                            [1] => Array
                                                (
                                                    [entity] => Germany
                                                )
                                            [2] => Array
                                                (
                                                    [entity] => Switzerland
                                                )
                                        )
                                )
                            [1] => Array
                                (
                                    [entity] => East
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [entity] => Czech Republic
                                                )
                                            [1] => Array
                                                (
                                                    [entity] => Slovakia
                                                )
                                            [2] => Array
                                                (
                                                    [entity] => Poland
                                                )
                                        )
                                )
                        )
                )
        )
)

我已经尝试为此编写递归函数:

function ListArray($top_elt)
{
    $myarray=array();   
    $myarray[0]['entity']=$top_elt;
        // foreach element of the list
        foreach($children as $child) 
        {
                //element is consolidated : call the recursive function to list the children
                // this is the function that returns whether the element is consolidated or not
                if(palo_etype($connect,'Demo','Regions',$child)=='consolidated')
                {
            $myarray[0]['children'][]['entity']=$child;
            ListArray($child);
        }
                else //basis element
                {
            $myarray[0]['children'][]['entity']=$child;
                }
        }        
        return $myarray;
}

但它只返回欧洲,西方和东方...

你知道吗?

提前感谢您的支持!

干杯, laloune

0 个答案:

没有答案