0

我需要从“平面”数组创建一个巨大的多维关联数组,以使其符合 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;
}

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

你有想法吗 ?

提前感谢您的支持!

干杯,拉鲁恩

4

0 回答 0