PHP从字符串构造嵌套数组

时间:2014-10-31 22:18:02

标签: php arrays

我的数据如下:

1          Root Catalog 
1/2        Main Website
1/2/4      Cold Beverages
1/2/4/19   Pop - Canned
1/2/4/20   Pop - Natural Low Calorie
1/2/4/21   Pop - Bottled - Large Plastic
1/2/4/22   Pop - Bottled - Small Plastic

需要将其变成一个看起来像的数组:

array(
        1 => array(
            'name' => 'Root Catalog',
            2      => array(
                'name' => 'Main Website',
                4      => array(
                    'name' => 'Cold Beverages',
                    19     => array(
                       'name' => 'Pop - Canned'
                    )
                    /*more numbers here*/
                )
            )
        )
    )

我无法弄清楚如何在保持数据完整性的同时将数组分配给父数组。 我遇到的问题是我可以嵌套1- n 级别。我试过爆炸字符串并使用递归重建它,但它永远不会正确。我有什么想法可以试试吗?

编辑:到目前为止的最佳尝试:

foreach ($categories as $category) {
        $paths = explode('/', $category->getPath());
        $paths = array_reverse($paths);

        $temp = array('name' => $category->getName());

        foreach ($paths as $key => $value) {
            $temp = array($value => $temp);
        }

        $data = array_merge($data, $temp);
    }

1 个答案:

答案 0 :(得分:3)

$data = array(
 '1'         =>   'Root Catalog', 
 '1/2'       =>   'Main Website',
 '1/2/4'     =>   'Cold Beverages',
 '1/2/4/19'  =>   'Pop - Canned',
 '1/2/4/20'  =>   'Pop - Natural Low Calorie',
 '1/2/4/21'  =>   'Pop - Bottled - Large Plastic',
 '1/2/4/22'  =>   'Pop - Bottled - Small Plastic'
);

$out = array();

foreach($data as $route => $value) // take every element of $data
{
    $path = explode('/', $route);  // split the route into elements 
    $current = &$out;              // get pointer to the root of the output array
    foreach($path as $level)       // traverse through the path
    {
       if (!isset($current[$level]))  // if branch does not exist, create it
          $current[$level] = array();

       $current = &$current[$level];  // set current pointer to that branch
    }
    $current['name'] = $value; // set leaf at the end of the branch
} 

print_r($out);  // output result

结果:

Array
(
    [1] => Array
        (
            [name] => Root Catalog
            [2] => Array
                (
                    [name] => Main Website
                    [4] => Array
                        (
                            [name] => Cold Beverages
                            [19] => Array
                                (
                                    [name] => Pop - Canned
                                )

                            [20] => Array
                                (
                                    [name] => Pop - Natural Low Calorie
                                )

                            [21] => Array
                                (
                                    [name] => Pop - Bottled - Large Plastic
                                )

                            [22] => Array
                                (
                                    [name] => Pop - Bottled - Small Plastic
                                )

                        )

                )

        )

)