0
我有以下函数可以帮助我获取类别名称。函数打印一个数组,但调用时返回null php
public function getParent($categoryId, $keepData) {
$parentCategory = Category::select('category_id', 'parent_id', 'title')
->where('category_id', $categoryId)->first();
if($parentCategory->parent_id > 0) {
$parent = Category::select('category_id','parent_id','title')
->where('category_id',$parentCategory->parent_id)
->first();
$keepData[$parent->category_id] = $parent->title;
$this->getParent($parent->category_id, $keepData);
} else {
//print_r($keepData);
return $keepData;
}
}
当我做print_r
里面的功能它打印一个数组。
array:2 [▼
3 => "Stock Boxes"
1 => "Boxes"
]
但调用其他功能,因为这功能,
$keepData = array();
$breadcrumb = $this->getParent($categoryId, $keepData);
print_r($breadcrumb);
然后返回null
。
我错过了什么步骤。任何帮助都会很棒。
什么是在你的预期数据如果'parnet_id = 0'' $ keepData'将会返回null;' –
将空'$ keepData'传递给'getParent()'的目的是什么? –
尝试在您的方法中传递'$ keepData'作为参考变量,它应该返回您的值。 –