ツリー ノードの親を再帰的に検索し、その親カテゴリを配列で返そうとしています。
この関数は、最終的に返される各親の配列を受け取り、自分自身に渡します。
この配列には要素がありますが、関数の外で見た場合の return の前のステートメントは ですnul
。
それを機能させるために、参照によってパラメーターを作成しました。しかし、なぜそれは常にnul
ですか?
これが私のコードです:
function getParent($id,$parents){ // to work changed this to getParent($id,&$parents)
if($id < 2) { // 1 is the Top of the Tree , so job is done
return $string;
}
$child = DB::fetchExecute((object)array( // pdo query for category to get parents
'sql' => "category",
'values'=> array($id),
'single'=> 1,
'debug' => 0)
);
$parent = DB::fetchExecute((object)array( // pdo query for parents info
'sql' => "category",
'values'=> array($child->native_parent_category_id),
'single'=> 1,
'debug' => 0)
);
$string[]= "<li>$parent->name ($parent->native_category_id)</li>
";
getParent($parent->native_category_id , $parents);
}
// call function
$array = array();
$returnString = getParent($id,$string);
var_dump($returnString,$array); // will both be NULL, or if called by Reference $array has the goods
?>