gpt4 book ai didi

php - 数组最后返回一个值

转载 作者:行者123 更新时间:2023-11-30 22:13:13 25 4
gpt4 key购买 nike

什么是将父项值放入同一个数组 (Const) 中的最佳方法,因为此代码仅返回一个值

public function divisionParent($name)
{
$path = array();
$path[] = $name;
$div = CsiCategory::where('name', $name)->first();
$parent_id = $div->parent_id;


if ($parent_id != 0) {
$name = CsiCategory::where('id', $parent_id)->first();
$this->divisionParent($name->name);
}


return $path;

}

最佳答案

也许是这样的?

public function divisionParent($name, $path = [])
{
// Append to $path arr.
array_push($path, $name);

// Get division by name.
$div = CsiCategory::where('name', $name)->first();

// Check for existing parent division by id.
if (isset($div)) {
if ($div->parent_id != 0) {
$divParent = CsiCategory::where('id', $div->parent_id)->first();
}
}

return isset($divParent) ? $this->divisionParent($divParent->name, $path) : $path;
}

关于php - 数组最后返回一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39336946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com