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.
我一直致力于递归函数,以便从几个级别的子类别到类别获取面包屑信息。我能够检索这个信息,但是一旦我用这个函数返回一个数组数组,我就不能用print_r来查看内容。
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:
如果我在函数中取消注释print_r(...),它将正确显示数组数组:
( [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?
因此,当我在最后执行print_r时,在调用该函数之后,我希望打印相同的数组数组。但我一无所获。我想我已经测试了功能中可能出错的一切。打印返回值时,我真的看不出有什么问题。你能帮我吗?
PS: In case anyone needs it, the get_subcat_id()
function goes as follows:
PS:如果有人需要它,get_subcat_id()函数如下:
//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 个解决方案
#1
5
You're not returning anything in the first branch of the if
. Try:
你没有在if的第一个分支中返回任何内容。尝试:
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.
当您在此函数中递归并到达递归的末尾时,它将返回反转的数组。但由于递归的调用者不使用return,新数组将被丢弃,并且永远不会返回到原始调用者。
#2
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:
我认为代码中的问题是,如果你在递归循环中执行print_r,它会按预期显示当前的痕迹。但是如果你从外面做,那就不会因为你没有从你的递归链中返回任何东西(你正在调用函数,但你没有返回最后一次调用)。尝试返回递归函数:
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);
}
}
#3
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.
使用print_r()时,将布尔值true作为第二个参数传递。这会将值打印为字符串。您将以这种方式获得更准确的调试数据,因为您将能够看到新行和其他值,否则这些值不会很明显。我会将它与error_log()函数结合使用,该函数将错误输出到php错误日志。这样您的调试就不会中断程序的正常流程。
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.
如果您不确定PHP错误日志的位置,请检查您的php.ini。如果错误日志指令未在php.ini中正确配置,并且您将PHP作为Apache模块运行,则它将默认为Apache错误日志。
I suspect that you will see something that you didn't see before when you do it this way.
我怀疑当你这样做的时候,你会看到一些你以前没有看到过的东西。
Hope this helps :-)
希望这可以帮助 :-)
#4
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.
我的猜测是它与你的函数递归有关,你用array_reverse返回数组。当它到达结束时,数组被反转并且PHP在重建原始数组时遇到了麻烦。
#1
5
You're not returning anything in the first branch of the if
. Try:
你没有在if的第一个分支中返回任何内容。尝试:
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.
当您在此函数中递归并到达递归的末尾时,它将返回反转的数组。但由于递归的调用者不使用return,新数组将被丢弃,并且永远不会返回到原始调用者。
#2
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:
我认为代码中的问题是,如果你在递归循环中执行print_r,它会按预期显示当前的痕迹。但是如果你从外面做,那就不会因为你没有从你的递归链中返回任何东西(你正在调用函数,但你没有返回最后一次调用)。尝试返回递归函数:
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);
}
}
#3
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.
使用print_r()时,将布尔值true作为第二个参数传递。这会将值打印为字符串。您将以这种方式获得更准确的调试数据,因为您将能够看到新行和其他值,否则这些值不会很明显。我会将它与error_log()函数结合使用,该函数将错误输出到php错误日志。这样您的调试就不会中断程序的正常流程。
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.
如果您不确定PHP错误日志的位置,请检查您的php.ini。如果错误日志指令未在php.ini中正确配置,并且您将PHP作为Apache模块运行,则它将默认为Apache错误日志。
I suspect that you will see something that you didn't see before when you do it this way.
我怀疑当你这样做的时候,你会看到一些你以前没有看到过的东西。
Hope this helps :-)
希望这可以帮助 :-)
#4
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.
我的猜测是它与你的函数递归有关,你用array_reverse返回数组。当它到达结束时,数组被反转并且PHP在重建原始数组时遇到了麻烦。