0

I am trying to write a recursive function that drills back to the root category of a nest of unknown depth.

[TABLE]

   cat_id | cat_name | cat_parent | cat_slug  
   //each category with a cat_parent of 0 is a root category

[/TABLE]

Example SQL result:

Array
(
[0] => Array
    (
        [cat_id] => 17
        [cat_name] => another-test-category
        [cat_parent] => 16
        [cat_slug] => Another test category
    )

)

Function:

function breadcrumb($cat_id){

 $cat_nest =     
            SELECT *
            FROM table
            WHERE cat_id = '$cat_id' 
           //returns 1 row;

  $cat_array[$cat_id] = $cat_nest[0];  

   if($cat_nest[0]['cat_parent'] != 0){

   $cat_array[] = breadcrumb($cat_nest[0]['cat_parent']);
   } 

   return $cat_array;

 }

It is outputting:

Array
(
[17] => Array
(
    [cat_id] => 17
    [cat_name] => test.example.1
    [cat_parent] => 16
    [cat_slug] => Test Example 1
)

[18] => Array
(
    [16] => Array
        (
            [cat_id] => 16
            [cat_name] => test.example.2
            [cat_parent] => 15
            [cat_slug] => Test Example 2
        )

    [17] => Array
        (
            [15] => Array
                (
                    [cat_id] => 15
                    [cat_name] => test.example.3
                    [cat_parent] => 6
                    [cat_slug] => Test Example 3
                )

            [16] => Array
                (
                    [6] => Array
                        (
                            [cat_id] => 6
                            [cat_name] => test.example.4
                            [cat_parent] => 2
                            [cat_slug] => Test Example 4
                        )

                    [7] => Array
                        (
                            [2] => Array
                                (
                                    [cat_id] => 2
                                    [cat_name] => test.example.5
                                    [cat_parent] => 0
                                    [cat_slug] => Test Example 5
                                )

                        )

                )

        )

)

)

Desired output:

Array
(
[17] => Array
(
[cat_id] => 17
[cat_name] => test.example.1
[cat_parent] => 16
[cat_slug] => Test Example 1
)

[16] => Array
(
[cat_id] => 16
[cat_name] => test.example.2
[cat_parent] => 15
[cat_slug] => Test Example 2
)


[15] => Array
(
[cat_id] => 15
[cat_name] => test.example.3
[cat_parent] => 6
[cat_slug] => Test Example 3
)


[6] => Array
(
[cat_id] => 6
[cat_name] => test.example.4
[cat_parent] => 2
[cat_slug] => Test Example 4
)

[2] => Array
(
[cat_id] => 2
[cat_name] => test.example.5
[cat_parent] => 0
[cat_slug] => Test Example 5
)

)
karel
  • 5,489
  • 46
  • 45
  • 50
chris
  • 2,913
  • 4
  • 43
  • 48
  • This looks very similar to your [previous question](http://stackoverflow.com/questions/23551451/multi-dimensional-array-of-unknown-depth-to-one-dimensonal-array-and-relevant-ke). And what is `$cat_nest` here? Where is it getting initialized? – Amal Murali May 08 '14 at 21:14
  • @AmalMurali the previous question was flattening the array after the fact. I am wondering if that step can be avoided by fixing the recursive function that outputs it. $cat_nest is the result of an SQL query that returns 1 row at a time. sample output is shown in "example sql result" – chris May 08 '14 at 21:38

2 Answers2

0

I ran some tests and I think the solution can be implemented this way:

function breadcrumb($cat_id){

    $cat_nest =     
    SELECT *
    FROM table
    WHERE cat_id = '$cat_id' 
    //returns 1 row;

    $cat_array[$cat_id] = $cat_nest[0];  

    if($cat_nest[0]['cat_parent'] != 0){

        $cat_array = array_merge( $cat_array, breadcrumb($cat_nest[0]['cat_parent']) );
    } 

    return $cat_array;

}

This would maintain the IDs exactly as you need them, since this would only be merging arrays and not creating new indexes with $cat_array[]

Ozmah
  • 373
  • 1
  • 8
0

Consider using a do-while-loop:

function breadcrumb($cat_id) {
 $cat_array = array();
 do {
   $cat_nest =     
            SELECT *
            FROM table
            WHERE cat_id = '$cat_id';
   $cat_array[$cat_id] = $cat_nest[0];
   $cat_id = $cat_nest[0]['catparent'];
   } while ($cat_id != 0)

 return $cat_array;
 }

This way you'll also be able to easily identify the 'first' breadcrumb, and add extra code for it, like declaring the array.

Thisis
  • 17
  • 3
  • 1
    thank for the answer, your latest code works well but @Ozmah inspired this question and as a result answered two of my questions. Your solution may in fact be more efficient as it does not use array_merge. – chris May 08 '14 at 22:25
  • I opted for that solution to maintain it a recursive function but I do admit that this solution seems much better performance wise :) – Ozmah May 08 '14 at 23:29