Vincent Decaux
Vincent Decaux

Reputation: 10714

Get simple recursive from table with id_parent

I guess my brain starts to slow down, but I want to get a recursive array of my results from this table :

|id|int(11)|
|name|varchar(150)|
|type|tinyint(4)|
|id_parent|mediumint(9)|

|1|Marque forte|1|0|
|2|Communication|2|1|
|3|Respect du CI|0|2|
|4|Stratégie digitale|0|2
|5|Expérience de marque|2|1|
|6|Gastronomie|0|5|

I want an array like :

array('id' => array('name' => $name, 'childs' => array( ... ), 'id' => ...)

I just want to order my dataset in one array.

I tried this so far :

// returns array of objects - codeigniter result
$dbResult = $this->db->get_where('table')->result();

// will contains the array
$this->data['tree'] = array();
$this->getRecursive(0, 0, $dbResult);
....

private function getRecursive($parent, $niveau, $result)
{
    foreach ($result AS $row)
    {
        if ($parent == $row->id_parent)
        {
            $this->data['tree'][] = array($row->name => $this->getRecursive($row->id, ($niveau + 1), $result));
        }
    }
}

Which gives me weird result ...

Upvotes: 1

Views: 243

Answers (2)

Atural
Atural

Reputation: 5439

You don't need a recursion here - the following should do the job

$arrTreeById = [];
//$arrData = $this->db->get_where('table')->result_array();

$arrData = [
    [
        'id' => 1,
        'name' => 'Marque forte',
        'type' => 2,
        'id_parent' => 0
    ],
    [
        'id' => 2,
        'name' => 'Communication',
        'type' => 2,
        'id_parent' => 1
    ],
    [
        'id' => 3,
        'name' => 'Respect du CI',
        'type' => 0,
        'id_parent' => 2
    ],
    [
        'id' => 4,
        'name' => 'Stratégie digitale',
        'type' => 0,
        'id_parent' => 2
    ],
    [
        'id' => 5,
        'name' => 'Expérience de marque',
        'type' => 2,
        'id_parent' => 1
    ],
    [
        'id' => 6,
        'name' => 'Gastronomie',
        'type' => 0,
        'id_parent' => 5
    ],
];

foreach($arrData AS $arrItem)
{
    $arrTreeById[$arrItem['id']] = $arrItem;
}

foreach($arrTreeById AS &$arrItem)
{
    if (isset($arrTreeById[$arrItem['id_parent']]))   
    {
        $arrTreeById[$arrItem['id_parent']]['arrChilds'][] = &$arrItem;
    }
    if ($arrItem['id_parent'] == 0) $intStartingKey = $arrItem['id'];
}

print_r($arrTreeById[$intStartingKey]);

Upvotes: 0

Kamrul Khan
Kamrul Khan

Reputation: 3350

Please try this. I have used the $source array for testing. Since I did not get my result set from an actual database, you will have to make some changes in order to adjust it with your code (eg. change $row['id_parent'] to $row->id_parent and so on). However, conceptually it should work.

<?php
        $source = [
                ['name' => 'A', 'id' => 1, 'id_parent' => 0],
                ['name' => 'B', 'id' => 2, 'id_parent' => 1],
                ['name' => 'C', 'id' => 3, 'id_parent' => 1],
                ['name' => 'D', 'id' => 4, 'id_parent' => 2],
                ['name' => 'E', 'id' => 5, 'id_parent' => 3],
                ['name' => 'F', 'id' => 5, 'id_parent' => 0],
        ];


        function getRecursive($source, $parent) {
            $result = [];
            foreach ($source as $row) {
                if ($parent == $row['id_parent']) {
                    $result[] = [
                                'id' => $row['id'],
                                'name' => $row['name'],
                                'childs' => getRecursive($source, $row['id'] )
                            ];
                        }
                }
                return $result;
        }

        print_r(getRecursive($source, 0));

Upvotes: 1

Related Questions