开发者

Missing nodes in multidimensional array tree

开发者 https://www.devze.com 2023-02-21 23:49 出处:网络
Trying to create tree in multidimensional arraywith the following code $source = array( (array(\'id\'=>406,\'parent\'=>0,\'title\'=>\'level_0_406\')),

Trying to create tree in multidimensional array with the following code

$source = array(
(array('id'=>406,'parent'=>0,'title'=>'level_0_406')),
(array('id'=>270,'parent'=>268,'title'=>'level_0_406_268_270')),
(array('id'=>271,'parent'=>268,'title'=>'level_0_406_268_271')),
(array('id'=>272,'parent'=>268,'title'=>'level_0_406_268_272')),
(array('id'=>273,'parent'=>268,'title'=>'level_0_406_268_273)')),
(array('id'=>269,'parent'=>268,'title'=>'level_0_406_268_269')),
(array('id'=>268,'parent'=>406,'title'=>'level_0_406_268')),
(array('id'=>407,'parent'=>406,'title'=>'level_0_406_407')),
(array('id'=>274,'parent'=>406,'title'=>'level_0_406_274')),
(array('id'=>500,'parent'=>407,'title'=>'level_0_406_407_500')),
);

$result = array();
$links = array(0=>&$result);

foreach ($source as &$element){
    $links[$element['id']] = &$element;     
    $links[$element['parent']]['childs'][$element['id']] = &$element;
}

But result array does not include several nodes of source array, viz. nodes with id=269,270,271,272,273.

Array
(
    [childs] => Array
        (
            [406] => Array
                (
                    [id] => 406
                    [parent] => 0
                    [title] => level_0_406
                    [childs] => Array
                        (
                            [268] => Array
                                (
                                    [id] => 268
                                    [parent] => 406
                                    [title] => level_0_406_268
                                )

                            [407] => Array
                                (
                                    [id] => 407
                                    [parent] => 406
                                    [title] => level_0_406_407
                                    [childs] => Array
                                        (
                                            [500] => Array
                                                (
                                                    [id] => 500
                                                    [parent] => 407
                                                    [title] => level_0_406_407_500
                                                )

                                        )

                                )

                            [274] => Array
                                (
                                    [id] => 274
                                    [parent] => 406
                                    [title] => level_0_406_274
                                )

                        )

                )

        )

)

I tried different code examp开发者_如何学运维les of tree generation but all of them have the same issue with source array like $source. Please help me understand such behavior.

Update Now i understand what is wrong with array. But what if i have such data in DB, how to make selection properly? $source array should be specially sorted before using tree generation function.


The original $source array values are not properly created. It should be:-

$source = array(
  (array('id'=>406,'parent'=>0,'title'=>'level_0_406')),
  (array('id'=>268,'parent'=>406,'title'=>'level_0_406_268')),
  (array('id'=>407,'parent'=>406,'title'=>'level_0_406_407')),
  (array('id'=>274,'parent'=>406,'title'=>'level_0_406_274')),
  (array('id'=>270,'parent'=>268,'title'=>'level_0_406_268_270')),
  (array('id'=>271,'parent'=>268,'title'=>'level_0_406_268_271')),
  (array('id'=>272,'parent'=>268,'title'=>'level_0_406_268_272')),
  (array('id'=>273,'parent'=>268,'title'=>'level_0_406_268_273)')),
  (array('id'=>269,'parent'=>268,'title'=>'level_0_406_268_269')),
  (array('id'=>500,'parent'=>407,'title'=>'level_0_406_407_500')),
);

If you look carefully, you will see that previously, only the child element of the parent element ID 407 was available, since the element ID 407 has been defined before the occurrence of the child element.

It is the de-facto of your coding logic to have the parent elements defined first followed by the definitions of the child elements. Also the general practice & standard has always been the same.

In my answer, I have changed the occurrence of the elements properly. This should work.

Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消