Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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()发出警告:为foreach()提供的参数无效_Php_Mysql_Pdo_Foreach - Fatal编程技术网

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

PHP嵌套的foreach()发出警告:为foreach()提供的参数无效,php,mysql,pdo,foreach,Php,Mysql,Pdo,Foreach,我有一个嵌套的while循环,用于获取大型菜单。while循环返回正确的数组数据。我试图在使用foreach()时反映这一点,但我发现了一个错误 这是我的PHP $cats = array(); $catSQL = $pdo->prepare("SELECT * FROM category"); $catSQL-> execute(); while($rowCat = $catSQL->fetch()) { $cat = array(); $cat['id']

我有一个嵌套的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>";
    }

  }

}
这是我在页面中获取的有错误的数据

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

在这里,您可以看到,尽管我得到了我需要的所有数据,但这个未定义的索引错误似乎不知从何而来。我不明白为什么会这样。请提供帮助。

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

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

谢谢那很有效。。我的错误。。我应该知道这一点:)
    while($childCatResult = $childCatSQL->fetch()) {
        $childCats[] = $childCatResult;
        $project['ccname'] = $childCats; // <-- wan't be set if there are no child categories
    }
    $childCat[] = $project;
    while($childCatResult = $childCatSQL->fetch()) {
        $childCats[] = $childCatResult;
    }
    $project['ccname'] = $childCats; // <---------- here
    $childCat[] = $project;