我有对象树:

array(4) (
    0 => object stdClass(14) {
        public id => string(1) "1"
        public parent_id => string(1) "0"
        public name => string(18) "Stationary engines"
        public uri => string(18) "stationary-engines"
        public created => string(19) "2012-11-19 15:15:34"
        public updated => NULL
        public subcategories => array(4) (
            0 => object stdClass(14) {
                public id => string(1) "5"
                public parent_id => string(1) "1"
                public name => string(6) "Yanmar"
                public uri => string(6) "yanmar"
                public created => string(19) "2012-11-19 15:23:36"
                public updated => NULL
                public subcategories => array(1) (
                    0 => object stdClass(14) {
                        public id => string(2) "15"
                        public parent_id => string(1) "5"
                        public name => string(18) "Yanmar subcategory"
                        public uri => string(18) "yanmar-subcategory"

                        public created => string(19) "2012-11-21 16:38:06"
                        public updated => NULL
                        public subcategories => array(1) (
                            0 => object stdClass(13) {
                                public id => string(2) "16"
                                public parent_id => string(2) "15"
                                public name => string(30) "Yanmar subcategory subcategory"
                                public uri => string(30) "yanmar-subcategory-subcategory"
                                public created => string(19) "2012-11-21 17:37:00"
                                public updated => NULL
                            }
                        )
                    }
                )
            }
            1 => object stdClass(13) {
                public id => string(1) "6"
                public parent_id => string(1) "1"
                public name => string(11) "Мercruiser"
                public uri => string(10) "mercruiser"
                public created => string(19) "2012-11-19 15:23:36"
                public updated => NULL
            }
            2 => object stdClass(13) {
                public id => string(1) "7"
                public parent_id => string(1) "1"
                public name => string(11) "Volvo-Penta"
                public uri => string(11) "volvo-penta"
                public created => string(19) "2012-11-19 15:24:49"
                public updated => NULL
            }
            3 => object stdClass(13) {
                public id => string(1) "8"
                public parent_id => string(1) "1"
                public name => string(27) "Basic configuration engines"
                public uri => string(27) "basic-configuration-engines"
                public created => string(19) "2012-11-19 15:24:49"
                public updated => NULL
            }
        )
    }
    1 => object stdClass(14) {
        public id => string(1) "2"
        public parent_id => string(1) "0"
        public name => string(10) "Generators"
        public uri => string(10) "generators"
        public created => string(19) "2012-11-19 15:15:58"
        public updated => NULL
        public subcategories => array(4) (
            0 => object stdClass(13) {
                public id => string(1) "9"
                public parent_id => string(1) "2"
                public name => string(6) "Diesel"
                public uri => string(6) "diesel"
                public created => string(19) "2012-11-19 15:34:38"
                public updated => NULL
            }
            1 => object stdClass(13) {
                public id => string(2) "10"
                public parent_id => string(1) "2"
                public name => string(8) "Gasoline"
                public uri => string(8) "gasoline"
                public created => string(19) "2012-11-19 15:34:38"
                public updated => NULL
            }
            2 => object stdClass(13) {
                public id => string(2) "11"
                public parent_id => string(1) "2"
                public name => string(6) "Kohler"
                public uri => string(6) "kohler"
                public created => string(19) "2012-11-19 15:35:35"
                public updated => NULL
            }
            3 => object stdClass(13) {
                public id => string(2) "12"
                public parent_id => string(1) "2"
                public name => string(13) "Fischer Panda"
                public uri => string(13) "fischer-panda"
                public created => string(19) "2012-11-19 15:36:14"
                public updated => NULL
            }
        )
    }
    2 => object stdClass(14) {
        public id => string(1) "3"
        public parent_id => string(1) "0"
        public name => string(14) "Boat equipment"
        public uri => string(14) "boat-equipment"
        public created => string(19) "2012-11-19 15:16:59"
        public updated => NULL
        public subcategories => array(1) (
            0 => object stdClass(13) {
                public id => string(2) "13"
                public parent_id => string(1) "3"
                public name => string(11) "Subcategory"
                public uri => string(11) "subcategory"
                public created => string(19) "2012-11-19 15:37:12"
                public updated => NULL
            }
        )
    }
    3 => object stdClass(14) {
        public id => string(1) "4"
        public parent_id => string(1) "0"
        public name => string(11) "Spare parts"
        public uri => string(11) "spare-parts"
        public created => string(19) "2012-11-19 15:16:59"
        public updated => NULL
        public subcategories => array(1) (
            0 => object stdClass(13) {
                public id => string(2) "14"
                public parent_id => string(1) "4"
                public name => string(11) "Consumables"
                public uri => string(11) "consumables"
                public created => string(19) "2012-11-19 15:37:39"
                public updated => NULL
            }
        )
    }
)


我需要将其转换为关联数组,如下所示:

array(
    '1' => 'category name',
    '3' => '   subcategory name',
    '4' => '      subcategory name',
    '2' => 'category2 name',
    '5' => '   subcategory2 name',
);


每个子类别都应以空格缩进。
但是我对递归并不是很熟悉,需要一些帮助,如果您能帮助我,我将不胜感激。
提前致谢。

更新:
我最终得到以下结果:

$options = array();
$this->to_array($tree, $options, '   ', 0);

function to_array($tree, & $target, $indention = '', $lvl = 0)
{
    foreach($tree as $leaf)
    {
        $target[$leaf->id] = str_repeat($indention, $lvl).$leaf->name;

        if (isset($leaf->subcategories))
        {
            $this->to_array($leaf->subcategories, $target, $indention, $lvl + 1);
        }
    }
}


有什么改进建议吗?

最佳答案

我会按照以下方式寻求帮助:

function printArray($arr, &$target, $header = '') {
  foreach($arr as $category) {
    $target[$category->id] = $header . $category->name;
    printArray($category->subcategories, $target, $header . ' ');
  }
}

// And then, from your source
$targetArray = array();
printArray($source, $targetArray);


那不会解决可能的冲突,但是如果您的数据来自单个表,则不会发生冲突。

要进行更多自定义,您可以将标头修改作为参数传递给递归函数,例如:

function printArray($arr, &$target, $header = '', $headerIncrement = ' ') {
  foreach($arr as $category) {
    $target[$category->id] = $header . $category->name;
    printArray($category->subcategories, $target, $header . $headerIncrement, $headerIncrement);
  }
}


那可以让你用

printArray($source, $target, '', '   ');
//or even
printArray($source, $target, '', '<span class="spacer">&nbsp;</span>');


并在CSS中添加样式以更改所需的间距。

关于php - 将对象的数组树分为具有级别的关联数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13563927/

10-17 02:52