dtu15253 2012-07-12 18:41
浏览 31

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

I need to write a recursive function to search through a set of parent and children link headers, then get the names of embedded key values per header. Ex. Clothing->Men->Shoes. Now, each category has an unknown number of values attached to them. I have a function that can successfully echo all of these values recursively through the parents. But when I try to get the return value from the function, it's missing some, and I can't understand why : /.

Code is below

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;
    }

So this displays all the names through echo to the screen during the functions run, and the array WITHIN the function can be echo'd to the screen as well. But when I try to return all of the names from returned result in another page, I'm missing a bunch of names.

EDIT It looks like the $families variable is not persisting through each recursive call to getFamilies, and is only returning the children at the end, instead of the children and all their parents.

  • 写回答

1条回答 默认 最新

  • duanjue2560 2012-07-13 14:52
    关注

    Fixed. problem was I was trying to clean up the array in the same recursive statement and it was throwing everything off since the cleaning "helper method" was being returned and not the full associative array of families. Sigh @ me for being oblivious.

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大