Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在foreach循环laravel 5.8时向数组添加数组或行_Php_Laravel 5_Recursion - Fatal编程技术网

Php 如何在foreach循环laravel 5.8时向数组添加数组或行

Php 如何在foreach循环laravel 5.8时向数组添加数组或行,php,laravel-5,recursion,Php,Laravel 5,Recursion,我需要得到所有的产品类别和子类别时,点击父类别 所以我做了一个循环或递归循环来获取category和subcategory的所有id来搜索它的产品 public function tree($category, $cats = array()) { $items = category_model::select('id')->where('parent_id', $category)->get(); foreach ($items as $key=>$value

我需要得到所有的产品类别和子类别时,点击父类别

所以我做了一个循环或递归循环来获取category和subcategory的所有id来搜索它的产品

public function tree($category, $cats = array())
{
    $items = category_model::select('id')->where('parent_id', $category)->get();
    foreach ($items as $key=>$value)
    {
        //$cats = $value;
        $cats = Arr::add($cats, 'id', $value);
        self::tree($value, $cats);
    }
    return $cats;
}

public function allproduct(Request $request)
{
    return self::tree($request->id);
}
我已经尝试过这个代码,但与我们的结果循环

我需要添加此所有id,以便通过此数组搜索产品

修复您的foreach循环

foreach ($items as $key=$value)
应该是

foreach ($items as $key => $value)
我不能评论静态Arr函数(我猜这是一个很小的问题?因为到处都滥用静态?

修复您的foreach循环

foreach ($items as $key=$value)
应该是

foreach ($items as $key => $value)

我不能评论静态Arr函数(我猜这是一个拉维函数?因为到处都滥用静态?

您可以改进自己的代码,并通过一次获取所有类别ID使其工作,而不是在那里为每个循环创建一个循环

此外,您还缺少一个终止条件,这是使用递归函数时必须满足的条件

此外,如果已经处理了相同的ID,则不需要一次又一次地处理它们

考虑到所有这些要点,请执行以下操作:

public function tree($cats, $alreadyProcessedCats = [])
{
    if (empty($cats)) {
        return [];
    }
    $newCatIds = [];
    foreach ($cats as $catId) {
        //do not process it if it was alreadt processed
        if (in_array($catId, $alreadyProcessedCats)) {
            continue;
        }
        //fetch all the categories id where parent id is one of the id which is present in the $cats
        $items = category_model::where('parent_id', $category)->pluck('id');
        if (empty($items)) {
            continue;
        }
        $items = $items->toArray();
        $newCatIds = array_merge($newCatIds, $items);
    }
    //terminal condition
    if (empty($newCatIds)) {
        return $cats;
    }
    $newCats = array_merge($cats, $newCatIds);
    return self::tree($newCats, $cats);
}

public function allproduct(Request $request)
{
    $allCategoriesIds = [$request->id];
    $allCategoriesIds = self::tree($allCategoriesIds);
}

您可以通过一次获取所有类别ID来改进自己的代码并使其工作,而不是在那里为每个循环创建一个for循环

此外,您还缺少一个终止条件,这是使用递归函数时必须满足的条件

此外,如果已经处理了相同的ID,则不需要一次又一次地处理它们

考虑到所有这些要点,请执行以下操作:

public function tree($cats, $alreadyProcessedCats = [])
{
    if (empty($cats)) {
        return [];
    }
    $newCatIds = [];
    foreach ($cats as $catId) {
        //do not process it if it was alreadt processed
        if (in_array($catId, $alreadyProcessedCats)) {
            continue;
        }
        //fetch all the categories id where parent id is one of the id which is present in the $cats
        $items = category_model::where('parent_id', $category)->pluck('id');
        if (empty($items)) {
            continue;
        }
        $items = $items->toArray();
        $newCatIds = array_merge($newCatIds, $items);
    }
    //terminal condition
    if (empty($newCatIds)) {
        return $cats;
    }
    $newCats = array_merge($cats, $newCatIds);
    return self::tree($newCats, $cats);
}

public function allproduct(Request $request)
{
    $allCategoriesIds = [$request->id];
    $allCategoriesIds = self::tree($allCategoriesIds);
}

很多打字错误!您在
:)中遇到了一些问题,这是因为“添加问题”错误,只是打字错误太多了!你在
>
:)上遇到了一些问题,那是因为add-question错了,只是他的代码中有更多的拼写错误:)我不认为修复这些拼写错误是OP想要的,他的代码中有更多的拼写错误:)我不认为修复这些拼写错误是OP想要的SQLSTATE[HY000]:一般错误:2031(SQL:从
category
中选择
id
,其中
parent\u id
=?)在此行------>$items=category\u model::where('parent\u id',$newCatIds)->pull('id');SQLSTATE[HY000]:一般错误:2031(SQL:从
category
where
parent\u id
=?)在这一行----->$items=category\u model::where('parent\u id',$newCatIds)->pull('id');