Loveleen Kaur
Loveleen Kaur

Reputation: 993

Value not getting assigned to a variable

I have an array of categories where id is the id of the category, parent denotes the parent id of the category (id 0 denotes the top most parent node) and value is the title of the array. The path is initially set to the id of the category. The array is as follows:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent] => 0
            [value] => Corporate Files
            [path] => 1
        )

    [1] => Array
        (
            [id] => 2
            [parent] => 0
            [value] => Products Files
            [path] => 2
        )

    [2] => Array
        (
            [id] => 3
            [parent] => 1
            [value] => Communications Materials
            [path] => 3
        )

    [3] => Array
        (
            [id] => 4
            [parent] => 1
            [value] => Group Technical
            [path] => 4
        )

    [4] => Array
        (
            [id] => 5
            [parent] => 1
            [value] => New Projects
            [path] => 5
        )

    [5] => Array
        (
            [id] => 6
            [parent] => 2
            [value] => Product Range
            [path] => 6
        )

    [6] => Array
        (
            [id] => 7
            [parent] => 2
            [value] => WL4
            [path] => 7
        )
);

I want to generate the paths of categories in the array. so the output should be

Array
(
    [0] => Array
        (
            [id] => 1
            [parent] => 0
            [value] => Corporate Files
            [path] => 1
        )

    [1] => Array
        (
            [id] => 2
            [parent] => 0
            [value] => Products Files
            [path] => 2
        )

    [2] => Array
        (
            [id] => 3
            [parent] => 1
            [value] => Communications Materials
            [path] => 1,3
        )

    [3] => Array
        (
            [id] => 4
            [parent] => 1
            [value] => Group Technical
            [path] => 1,4
        )

    [4] => Array
        (
            [id] => 5
            [parent] => 1
            [value] => New Projects
            [path] => 1,5
        )

    [5] => Array
        (
            [id] => 6
            [parent] => 2
            [value] => Product Range
            [path] => 2,6
        )

    [6] => Array
        (
            [id] => 7
            [parent] => 2
            [value] => WL4
            [path] => 2,7
        )
);

I wrote the following function.

   function findparent($id,$path){
        global $categories;
        global $catcnt;

        if($id==0){
            echo $path."<br />"; //this outputs path currently
            return $path;
        }
        for($i=0;$i<$catcnt;$i++){

            if($id==$categories[$i]['id']){
                $path=$id.",".$path;
                findparent($categories[$i]['parent'],$path);
            }
        }
    }

for($i=0;$i<count($categories);$i++){
             $categories[$i]['path']=(string)findparent($categories[$i]['parent'],$categories[$i]['id']); //this doesnt assign it currectly

    }

and the output is:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent] => 0
            [value] => Corporate Files
            [path] => 
        )

    [1] => Array
        (
            [id] => 2
            [parent] => 0
            [value] => Products Files
            [path] => 
        )

    [2] => Array
        (
            [id] => 3
            [parent] => 1
            [value] => Communications Materials
            [path] => 
        )

    [3] => Array
        (
            [id] => 4
            [parent] => 1
            [value] => Group Technical
            [path] => 
        )

    [4] => Array
        (
            [id] => 5
            [parent] => 1
            [value] => New Projects
            [path] => 
        )

    [5] => Array
        (
            [id] => 6
            [parent] => 2
            [value] => Product Range
            [path] => 
        )

    [6] => Array
        (
            [id] => 7
            [parent] => 2
            [value] => WL4
            [path] => 
        )
);

Where am I going wrong?

Upvotes: 2

Views: 110

Answers (2)

hakre
hakre

Reputation: 198118

As you don't need to deal with multiple levels, a function of it's own is a bit of an overhead as you could work straight on the array itself with a simple foreach:

foreach ($array as &$node)
{
        if ($node['parent'])
        {
                $node['path'] = $node['parent'] . ',' . $node['path'];
        }
}
unset($node);

However, you can put this into a function of it's own as well, but you won't need any global variables as far as I can see.

What you see here is simple string concatenation, this makes the array as you wrote you wanted it to have. My first comment was more meant to manage the structure with an n-depth not a 1-depth. Demo

Upvotes: 1

Borealid
Borealid

Reputation: 98509

findparent only returns if the id is zero.

You need a second return statement, before the recursive findparent call.

Upvotes: 2

Related Questions