make tree category

hi

i have this array:




$row = [

    ['id' => 1, 'parent_id' => 0, 'title' => 'A'],

    ['id' => 2, 'parent_id' => 1, 'title' => 'B'],

    ['id' => 3, 'parent_id' => 2, 'title' => 'C'],

    ['id' => 4, 'parent_id' => 0, 'title' => 'D'],

];



by getting help from this code:




function buildTree(array $elements, $parentId = 0) {

    $branch = array();

 

    foreach ($elements as $element) {

        if ($element['parent_id'] == $parentId) {

            $children = buildTree($elements, $element['id']);

            if ($children) {

                $element['children'] =  $children;

            }

            $branch[] = $element;

        }

    }

 

    return $branch;

}

$tree = buildTree($row);



made this array:




[

    0 => [

        'id' => 1

        'parent_id' => 0

        'title' => 'A'

        'children' => [

            0 => [

                'id' => 2

                'parent_id' => 1

                'title' => 'B'

                'children' => [

                    0 => [

                        'id' => 3

                        'parent_id' => 2

                        'title' => 'C'

                    ]

                ]

            ]

        ]

    ]

    1 => [

        'id' => 4

        'parent_id' => 0

        'title' => 'D'

    ]

]



finally i want to create this array, but cant :




// id => title

[

1 => 'A',

2 => '-B',

3 => '--D',

4 => 'E'

]



thanks in advanced




function buildTree(array $elements, $parentId = 0, $level = 0) {

    $branch = array();

	$level_ini = $level;

    foreach ($elements as $element) {

		$level = $level_ini;

        if ($element['parent_id'] == $parentId) {

			$element ['level'] = $level;

            $children = buildTree($elements, $element['id'], ++$level);

            if ($children) {

                $element['children'] =  $children;

            }

            $branch[] = $element;

        }

    }

    return $branch;

}

$tree = buildTree($row);



Now with ‘level’ is easy to do it.

thanks, as i told i want to make this result in php array :




// id => title

[

1 => 'A',

2 => '-B',

3 => '--D',

4 => 'E'

]



following piece of code make above result but with wrong ids:




function sequential($nodes, $depth='')

    {

        $oa = array();

        foreach($nodes as $node)

            {

                $oa[] = $depth.$node['title'];

                if(isset($node['children']))

                    foreach(sequential($node['children'],$depth.'-') as $internalNode)

                        $oa[] = $internalNode;

            }

        return $oa;

    }



i want to use the id that that I get from db

thanks in advanced

Try with this,




function buildTree(array $elements, $parentId = 0, $level = '') {

    $branch = array();

    $level_ini = $level;

    foreach ($elements as $element) {

	$level = $level_ini;

        if ($element['parent_id'] == $parentId) {

		$element ['title'] = $level.$element ['title'];

		$level .= '_';

                $children = buildTree($elements, $element['id'], $level);

   		$branch[$element['id']] = $element['title'];

                if ($children) {

			$branch = yii\helpers\ArrayHelper::merge($branch,$children);

                }

        }

    }

    return $branch;

}



thanks rahif

i have this :




function sequential($nodes, $depth='')

{

    $oa = array();

    foreach($nodes as $node)

    {

        $oa[] = $depth.$node['title'];

        if(isset($node['children']))

            foreach(sequential($node['children'],$depth.'-') as $internalNode)

                $oa[] = $internalNode;

    }

    return $oa;

}


function buildTree(array $elements, $parentId = 0, $level = '') {

    $branch = array();

    $level_ini = $level;

    foreach ($elements as $element) {

        $level = $level_ini;

        if ($element['parent_id'] == $parentId) {

                $element['title'] = $level.$element ['title'];

                $level .= '_';

                $children = buildTree($elements, $element['id'], $level);

                $branch[$element['id']] = $element['title'];

                if ($children) {

                        $branch = yii\helpers\ArrayHelper::merge($branch,$children);

                }

        }

    }

    return $branch;




var_dump(sequential(buildTree($row)));


}



but this error happened in foreach loop in sequential function :( :




Illegal string offset 'title'




thanks for you spending time

Only one function, like this


var_dump(buildTree($row));

made this result:




array (size=4)

  1 => string 'A' (length=1)

  2 => string 'B' (length=1)

  3 => string 'C' (length=1)

  4 => string 'D' (length=1)



if i do this:




$row = [

    ['id' => 1, 'parent_id' => 0, 'title' => 'A'],

    ['id' => 2, 'parent_id' => 1, 'title' => 'B'],

    ['id' => 3, 'parent_id' => 2, 'title' => 'C'],

    ['id' => 4, 'parent_id' => 0, 'title' => 'D'],

    ['id' => 5, 'parent_id' => 4, 'title' => 'E'],

    ['id' => 6, 'parent_id' => 3, 'title' => 'F'],

];

function buildTree(array $elements, $parentId = 0, $level = '') {

    $branch = array();

    $level_ini = $level;

    foreach ($elements as $element) {

        $level = $level_ini;

        if ($element['parent_id'] == $parentId) {

                $element['title'] = $level.$element ['title'];

                $level .= '_';

                $children = buildTree($elements, $element['id'], $level);

                $branch[$element['id']] = $element['title'];

                if ($children) {

                        $branch = yii\helpers\ArrayHelper::merge($branch,$children);

                }

        }

    }

    return $branch;

}

var_dump(buildTree($row));



i have this:




[

    1 => 'A'

    2 => '_B'

    3 => '__C'

    6 => '___F'

    4 => 'D'

    5 => '_E'

]



curious case :blink:

oh yes, work with the array

but fail on my table in db, this is the schema:




CREATE TABLE `category` (

 `id` int(11) NOT NULL AUTO_INCREMENT,

 `category_id` int(11) DEFAULT NULL,

 `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,

 PRIMARY KEY (`id`)

)






$row = Category::find()->asArray()->all();

var_dump(buildTree($row));



but first change parent_id in function by category_id, that is your attribute.