PHP嵌套foreach()给出警告:为foreach()提供的参数无效

Posted

技术标签:

【中文标题】PHP嵌套foreach()给出警告:为foreach()提供的参数无效【英文标题】:PHP Nested foreach() gives Warning: Invalid argument supplied for foreach() 【发布时间】:2018-12-26 20:42:38 【问题描述】:

我有一个嵌套的 while 循环来获取超级菜单。 while 循环返回我真正想要的正确数组数据。我试图在使用 foreach() 时反映这一点,但我遇到了错误。

这是我的 php

$cats = array();
$catSQL = $pdo->prepare("SELECT * FROM category");
$catSQL-> execute();

while($rowCat = $catSQL->fetch()) 
    $cat = array();
    $cat['id']   = $rowCat['cat_id'];
    $cat['name'] = $rowCat['cat_name'];

    $childCat = array();

    $subCatSQL = $pdo->prepare("SELECT * FROM sub_category WHERE sc_cat = ".$rowCat['cat_id']);
    $subCatSQL-> execute();

    while($subCatResult = $subCatSQL->fetch()) 
        $subCatID = $subCatResult['sc_id'];
        $project = $subCatResult;

        $childCats = array();

        $childCatSQL = $pdo->prepare("SELECT * FROM child_category WHERE cc_subcat=".$subCatID);
        $childCatSQL-> execute();

        while($childCatResult = $childCatSQL->fetch()) 
            $childCats[] = $childCatResult;
            $project['ccname'] = $childCats;
        
        $childCat[] = $project;
    
    $cat['categories'] = $childCat;
    $cats[] = $cat;
    // echo "<pre>"; print_r($cat);



foreach($cats as $cat)
 echo "<p>".$cat['name']."</p>";

  foreach($cat['categories'] as $subcat)
    echo "<p>".$subcat['sc_name']."</p>";

    foreach($subcat['ccname'] as $childcat)
      echo "<p>".$childcat['cc_name']."</p>";
    

  


这是我来自print_r($cat);的数组数据

Array
(
    [id] => 1
    [name] => Computers
    [categories] => Array
        (
            [0] => Array
                (
                    [sc_id] => 1
                    [0] => 1
                    [sc_cat] => 1
                    [1] => 1
                    [sc_name] => Laptops
                    [2] => Laptops
                    [ccname] => Array
                        (
                            [0] => Array
                                (
                                    [cc_id] => 1
                                    [0] => 1
                                    [cc_subcat] => 1
                                    [1] => 1
                                    [cc_name] => Hewlett-Packard
                                    [2] => Hewlett-Packard
                                )

                            [1] => Array
                                (
                                    [cc_id] => 2
                                    [0] => 2
                                    [cc_subcat] => 1
                                    [1] => 1
                                    [cc_name] => Dell
                                    [2] => Dell
                                )

                            [2] => Array
                                (
                                    [cc_id] => 3
                                    [0] => 3
                                    [cc_subcat] => 1
                                    [1] => 1
                                    [cc_name] => Lenovo
                                    [2] => Lenovo
                                )

                            [3] => Array
                                (
                                    [cc_id] => 4
                                    [0] => 4
                                    [cc_subcat] => 1
                                    [1] => 1
                                    [cc_name] => Acer
                                    [2] => Acer
                                )

                        )

                )

            [1] => Array
                (
                    [sc_id] => 2
                    [0] => 2
                    [sc_cat] => 1
                    [1] => 1
                    [sc_name] => Desktops
                    [2] => Desktops
                    [ccname] => Array
                        (
                            [0] => Array
                                (
                                    [cc_id] => 5
                                    [0] => 5
                                    [cc_subcat] => 2
                                    [1] => 2
                                    [cc_name] => Dell
                                    [2] => Dell
                                )

                            [1] => Array
                                (
                                    [cc_id] => 6
                                    [0] => 6
                                    [cc_subcat] => 2
                                    [1] => 2
                                    [cc_name] => Lenovo
                                    [2] => Lenovo
                                )

                        )

                )

        )

)

Array
(
    [id] => 2
    [name] => Components
    [categories] => Array
        (
            [0] => Array
                (
                    [sc_id] => 3
                    [0] => 3
                    [sc_cat] => 2
                    [1] => 2
                    [sc_name] => Monitors
                    [2] => Monitors
                )

            [1] => Array
                (
                    [sc_id] => 4
                    [0] => 4
                    [sc_cat] => 2
                    [1] => 2
                    [sc_name] => Printers
                    [2] => Printers
                )

            [2] => Array
                (
                    [sc_id] => 5
                    [0] => 5
                    [sc_cat] => 2
                    [1] => 2
                    [sc_name] => Scanners
                    [2] => Scanners
                )

            [3] => Array
                (
                    [sc_id] => 6
                    [0] => 6
                    [sc_cat] => 2
                    [1] => 2
                    [sc_name] => Web Cameras
                    [2] => Web Cameras
                )

        )

)

这是我在页面中收到的错误数据。

Computers

Laptops

Hewlett-Packard

Dell

Lenovo

Acer

Desktops

Dell

Lenovo

Components

Monitors

Notice: Undefined index: ccname in E:\xampp\htdocs\flexicart\common-codes.php on line 54

Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\flexicart\common-codes.php on line 54

Printers

Notice: Undefined index: ccname in E:\xampp\htdocs\flexicart\common-codes.php on line 54

Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\flexicart\common-codes.php on line 54

Scanners

Notice: Undefined index: ccname in E:\xampp\htdocs\flexicart\common-codes.php on line 54

Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\flexicart\common-codes.php on line 54

Web Cameras

Notice: Undefined index: ccname in E:\xampp\htdocs\flexicart\common-codes.php on line 54

Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\flexicart\common-codes.php on line 54

在这里,您可以看到,尽管我获得了我需要的所有数据,但这个未定义索引错误似乎无处不在。我不明白为什么会这样。请帮忙。

【问题讨论】:

【参考方案1】:

您正在内部 while 循环中设置 $project['ccname']。但如果没有子类别,则不会进入循环。这也很浪费,因为您在每次循环迭代中一次又一次地覆盖它。

    while($childCatResult = $childCatSQL->fetch()) 
        $childCats[] = $childCatResult;
        $project['ccname'] = $childCats; // <-- wan't be set if there are no child categories
    
    $childCat[] = $project;

您需要在内部while循环之外设置$project['ccname'] = $childCats;

    while($childCatResult = $childCatSQL->fetch()) 
        $childCats[] = $childCatResult;
    
    $project['ccname'] = $childCats; // <---------- here
    $childCat[] = $project;

【讨论】:

谢谢..效果很好..我的错误..我应该知道这个..:)

以上是关于PHP嵌套foreach()给出警告:为foreach()提供的参数无效的主要内容,如果未能解决你的问题,请参考以下文章

foreach使用方法

for循环,for...in循环,forEach循环的区别

php中foreach循环遍历二维数组

详细讲解foreach循环的用法

视图中的嵌套 ForEach(和列表)给出了意想不到的结果

嵌套的 foreach 循环使用 if 条件创建重复记录 - PHP Laravel 8