0

I have been working on a recursive function to get breadcrumb information from subcategory to category for several levels. I am able to retrieve this info, but once I return an array of arrays with this function, I can't do print_r to see contents.

Let me show you the code before I rant on:

function breadcrumb_array ($id, $breadcrumb = array())  {
    $cat_info = get_subcat_data($id);
    if ($cat_info["parent_id"]>0) {
        $breadcrumb[] = $cat_info;
        breadcrumb_array($cat_info["parent_id"], $breadcrumb);
    } else {
        echo "About to return the value...";
        //print_r(array_reverse($breadcrumb)); 
        return array_reverse($breadcrumb);
    }
}
$breadcrumb = breadcrumb_array(8);
print_r($breadcrumb);

If I uncomment print_r(...) within the function, it will bring up the array of arrays properly:

( [0] => Array 
    ( [id] => 3
      [nombre] => Muebles
      [parent_id] => 1
      [parent_urlname] => 
      [parent_nombre] => Productos
    )
  [1] => Array
    ( [id] => 6
      [nombre] => Sillas
      [parent_id] => 3
      [parent_urlname] =>
      [parent_nombre] => Muebles
    )
  [2] => Array 
    (
      [id] => 8
      [nombre] => Sillas de cocina
      [parent_id] => 6
      [parent_urlname] => 
      [parent_nombre] => Sillas
    )
)

So when I do print_r at the very end, after calling the function, I expect to print the same array of arrays. But I get nothing. I think I have tested everything that can go wrong within the function. I really can't see what goes wrong when printing the returned value. Can you help?

PS: In case anyone needs it, the get_subcat_id() function goes as follows:

//Retrieves mroe info about a certain categories and its parent
function get_subcat_data ($id) {
    global $connection;
    $query =    "SELECT tblCategoria.id, tblCategoria.nombre, tblCategoria.parent_id, tblParents.urlname AS parent_urlname, tblParents.nombre AS parent_nombre
                FROM tblCategoria JOIN tblCategoria AS tblParents ON tblCategoria.parent_id = tblParents.id
                WHERE tblCategoria.id = {$id}
                LIMIT 1";
    $cats1 = mysql_query($query, $connection);
    confirm_query($cats1);
    $cats1 = mysql_fetch_assoc($cats1);
    return $cats1; 
}

4 Answers 4

5

You're not returning anything in the first branch of the if. Try:

if ($cat_info["parent_id"]>0) {
    $breadcrumb[] = $cat_info;
    return breadcrumb_array($cat_info["parent_id"], $breadcrumb);
}

When you recurse in this function, and reach the end of the recursion, it returns the reversed array. But since the caller of the recursion doesn't use return, the new array is discarded, and never returned to the original caller.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Barmar, this did the trick. If it's not too much trouble, could you explain why I needed this return? Without it, the function will print "About to return the value...", so I know for sure that it goes into the else branch. Why does it need to return the previous branch too?
I've added an explanation, does that help?
1

I think the problem in your code is that, if you do the print_r inside the recursive loop, it shows the current breadcrumb as expected. But if you do it from the outside, it won't because you aren't returning anything really from your chain of recursivity (you are doing calls to the function, but you're not returning the last call). Try returning your recursive function:

function breadcrumb_array ($id, $breadcrumb = array())  {
    $cat_info = get_subcat_data($id);
    if ($cat_info["parent_id"]>0) {
        $breadcrumb[] = $cat_info;

        // HERE
        return breadcrumb_array($cat_info["parent_id"], $breadcrumb);
    } else {
        return array_reverse($breadcrumb);
    }
}

Comments

1

When using print_r(), pass the Boolean value true as a second parameter. This will print the value as a string. You will get more accurate debug data this way because you will be able to see newlines and other values which would otherwise not be conspicuous. I would combine this with the error_log() function, which prints the error to the php error log. This way your debugging will not interrupt the normal flow of your program.

Here is an example of how you might use this technique:

error_log("About to return the result: " . print_r(array_reverse($breadcrumb), true));

If you're not sure of the location of your PHP error log, check your php.ini. If the error log directive is not properly configured in php.ini and you are running PHP as an Apache module it will default to the Apache error log.

I suspect that you will see something that you didn't see before when you do it this way.

Hope this helps :-)

Comments

0

Move the reverse outside of the function.

$breadcrumb = array_reverse (breadcrumb_array(8));

My guess is that it has something to do with your function being recursive, and you are returning the array reversed with array_reverse. When it gets to the end, the array is reversed and PHP is having trouble reconstructing the original array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.