Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将对象的数组树放入具有级别的关联数组中_Php_Mysql_Arrays_Recursion_Tree - Fatal编程技术网

Php 将对象的数组树放入具有级别的关联数组中

Php 将对象的数组树放入具有级别的关联数组中,php,mysql,arrays,recursion,tree,Php,Mysql,Arrays,Recursion,Tree,我有一个对象树: 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

我有一个对象树:

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>');
printary($source,$target,,'');
//甚至
printArray($source,$target,'','');

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

如果最后一个数组的所有键都是id,您将如何从该数组中获取第n个id?为什么您希望所有数组都是相同的结构,因此使用每个循环来循环数据。我认为您无法这样完成。每个
id
键都将覆盖上一个
id
键。您的所有键都属于idfinal array仅作为示例,而不是'id',将有类别的id,我已编辑问题谢谢,您已向我展示了灯光!我已经更新了问题,也许您可以对此代码进行一些改进?我只是更新了代码,我忘记了在递归调用中更新
$header
。现在,
$header
将随着每个级别而增长。这会将
添加到每个级别。您可以通过更改递归调用(从
$header.'
更改为
$header.'
)来更改行为。@UAMoto通过使用头和增量,您为每个调用保存了一个for循环(在
stru repeat
中),并且该循环不是必需的,因为您已经在循环中进行相同的计数。