PHP递归的奇怪返回值逻辑错误

时间:2012-07-12 18:41:01

标签: php arrays recursion qcodo

我需要编写一个递归函数来搜索一组父和子链接头,然后获取每个头的嵌入键值的名称。防爆。服饰>男人 - >鞋。现在,每个类别都附加了未知数量的值。我有一个函数可以通过父项递归地成功回显所有这些值。但是当我试图从函数中获取返回值时,它会丢失一些,我无法理解为什么:/。

代码低于

public function getFamilies($cat){
            $objCurrentCategory = Category::Load($cat); // creates a QCodo object of the passed category ID.

        $str_Query = "SELECT DISTINCT p.family
                      FROM xlsws_product p, xlsws_product_category_assn pc
                      WHERE p.rowid=pc.product_id
                      AND pc.category_id=".$cat; // sql query to retrieve all Families relating to this category.

        $objFamilyDb = Family::GetDatabase(); // retrieves the QCodo database object for Family to execute queries against.
        $objFamilies = Family::InstantiateDbResult($objFamilyDb->Query($str_Query)); // executes the query and saves the result.

        foreach($objFamilies as $family){ // for each family returned, get the family name and add it to the array of names.
            if ($family->Family !== ""){
                $families [] = $family->Family;
            }
        }

        if ($objCurrentCategory->ChildCount > 0){ // if current category has children, create a list of all children rowids.
            $str_Query = "SELECT rowid FROM xlsws_category
                          WHERE parent=".$objCurrentCategory->Rowid; // query to get all children of the category.
            $objChildCategoriesDb = Category::GetDatabase(); // retrieves the QCodo database object for Category to execute queries against.
            $objChildCategories = Category::InstantiateDbResult($objChildCategoriesDb->Query($str_Query)); // executes the query and saves the result.

            foreach($objChildCategories as $child){ // passes through the children to get their families.
                //$families [] = KG::getFamilies($child->Rowid);
                $childFam = KG::getFamilies($child->Rowid);
            }
        }

        $compiled = KG::compileFamilies($childFam); // helper function, not important.


        foreach($compiled as $compile){
            $families[] = $compile;
        }

        foreach($families as $familyt){ // this echo statement correctly displays all names.
            //echo ":".$familyt."<br />";
        }

        return $families;
    }

因此,在函数运行期间,这会通过echo显示所有名称,并且函数中的数组也可以回显到屏幕上。但是当我尝试将返回结果中的所有名称都返回到另一个页面时,我错过了一堆名字。

编辑看起来$ families变量没有通过每次递归调用getFamilies持久化,并且只返回子节点而不是子节点及其所有父节点。

1 个答案:

答案 0 :(得分:0)

固定。问题是我试图在同一递归语句中清理数组,因为返回清理“辅助方法”而不是完全关联的系列数组,所以它会抛弃所有内容。因为忘记而叹了口气。