Q: foreach in php(menu and sub menu) I have created a menu and submenu by oop php,
I have a problem with foreach loop.
what I am trying to achieve is to have my SUBMENU array loop only in the third element of my MENU array!!!
I hope I have described it well!
This is the foreach code:
<ul id="navigation">
    <?php
    $HeaderMenuOBJ = new Headermenu();
    $HeaderMenu = $HeaderMenuOBJ->getHeaderMenu();
    $SubHeaderMenu = $HeaderMenuOBJ->getHeaderSubMenu();
   //var_dump($SubHeaderMenu);
   //var_dump($HeaderMenu);
    foreach ($HeaderMenu as $row){
        ?>
        <li>
            <a class="active" 
                href="
                    <?php
                     echo $row['href'];
                    ?>
                ">
                <?php echo($row['content']);?>
                
            </a>
            <ul class="submenu">
                <?php 
                    foreach ($SubHeaderMenu as $row){
                ?>
                <li>
                    <a 
                        href="
                            <?php
                                echo $row['href'];
                            ?>
                            ">
                            <?php 
                                echo $row['content'];
                            ?>
                    </a>
                </li>
                <?php
                  }
                ?>
            </ul>

      </li>
        <?php
    }
 ?>
</ul>

and this is my DOM (kind of controller to read my data from database) :
class Headermenu extends dbh {
    public $main_query = "SELECT * FROM `header_menu` WHERE parent is null";
    public $sub_query = "SELECT * FROM `header_menu` WHERE parent = 3";
    public function getHeaderMenu(){
       $menu = parent::connect()->query($this->main_query);
        while($row = $menu->fetch_array()){
            $rows[] = $row;
            /*$submenu = parent::connect()->query(str_replace("@param",$header->PK_ID,$this->sub_query));
           while($sub_header = $submenu-> fetch_array()){
               $header->subheaders[]=$sub_header;
           }*/
           //$headers[] = $header;
       }
       return $rows;
    } 
    public function getHeaderSubMenu(){
        $sub_menu = parent::connect()->query($this->sub_query);
        while($row = $sub_menu->fetch_array()){
            $rows[] = $row;
       }
       return $rows;
    }
}


A: Direct answer:
First you need 2 different variables in outer and inner foreach: foreach ($HeaderMenu as $menuRow){ and foreach ($SubHeaderMenu as $subMenuRow){ now both have $row. You can do something like this: if ($menuRow['id'] == 3) { foreach ($SubHeaderMenu as $subMenuRow){ - so enclose inner foreach in this if and do it only if parent id is 3, but it would be better to fetch all header_menu rows, transform that data into nested array and display as is with dynamic structure.
Dynamic structure answer:
Fetch all menu items, create array with menus, and each menu will have its submenu items. Then displaying this is easy. I assume only 2 levels - menu and submenu.
sample data:
+----+-------+---------------+-----------+
| id | href  | content       | parent_id |
+----+-------+---------------+-----------+
|  1 | href1 | header1       |      NULL |
|  2 | href2 | header2       |      NULL |
|  3 | href3 | header3       |      NULL |
|  4 | href4 | subheader 1 1 |         1 |
|  5 | href5 | subheader 1 2 |         1 |
|  6 | href6 | subheader 2 1 |         2 |
+----+-------+---------------+-----------+

Code:
<?php

$mysqli = new mysqli("localhost", "zz", "zz", "zz");

// fetch all menu items
$query = 'select * from header_menu order by parent_id';
$result = $mysqli->query($query);
$data = $result->fetch_all(MYSQLI_ASSOC);

var_dump($data);

// build menu with menus and their submenus
$menu = [];
foreach ($data as $row) {
  if ($row['parent_id'] === null) {
    $menu[$row['id']] = $row;
    $menu[$row['id']]['submenus'] = [];
  } else {
    $menu[$row['parent_id']]['submenus'][] = $row;
  }
}

var_dump($menu);

// now display it in html or however you want
foreach ($menu as $item) {
  echo $item['content'].PHP_EOL;
  foreach ($item['submenus'] as $subitem) {
    echo '  '.$subitem['content'].PHP_EOL;
  }
}

$data:
array(6) {
  [0]=>
  array(4) {
    ["id"]=>
    string(1) "1"
    ["href"]=>
    string(5) "href1"
    ["content"]=>
    string(7) "header1"
    ["parent_id"]=>
    NULL
  }
  [1]=>
  array(4) {
    ["id"]=>
    string(1) "2"
    ["href"]=>
    string(5) "href2"
    ["content"]=>
    string(7) "header2"
    ["parent_id"]=>
    NULL
  }
  [2]=>
  array(4) {
    ["id"]=>
    string(1) "3"
    ["href"]=>
    string(5) "href3"
    ["content"]=>
    string(7) "header3"
    ["parent_id"]=>
    NULL
  }
  [3]=>
  array(4) {
    ["id"]=>
    string(1) "4"
    ["href"]=>
    string(5) "href4"
    ["content"]=>
    string(13) "subheader 1 1"
    ["parent_id"]=>
    string(1) "1"
  }
  [4]=>
  array(4) {
    ["id"]=>
    string(1) "5"
    ["href"]=>
    string(5) "href5"
    ["content"]=>
    string(13) "subheader 1 2"
    ["parent_id"]=>
    string(1) "1"
  }
  [5]=>
  array(4) {
    ["id"]=>
    string(1) "6"
    ["href"]=>
    string(5) "href6"
    ["content"]=>
    string(13) "subheader 2 1"
    ["parent_id"]=>
    string(1) "2"
  }
}

$menu:
array(3) {
  [1]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["href"]=>
    string(5) "href1"
    ["content"]=>
    string(7) "header1"
    ["parent_id"]=>
    NULL
    ["submenus"]=>
    array(2) {
      [0]=>
      array(4) {
        ["id"]=>
        string(1) "4"
        ["href"]=>
        string(5) "href4"
        ["content"]=>
        string(13) "subheader 1 1"
        ["parent_id"]=>
        string(1) "1"
      }
      [1]=>
      array(4) {
        ["id"]=>
        string(1) "5"
        ["href"]=>
        string(5) "href5"
        ["content"]=>
        string(13) "subheader 1 2"
        ["parent_id"]=>
        string(1) "1"
      }
    }
  }
  [2]=>
  array(5) {
    ["id"]=>
    string(1) "2"
    ["href"]=>
    string(5) "href2"
    ["content"]=>
    string(7) "header2"
    ["parent_id"]=>
    NULL
    ["submenus"]=>
    array(1) {
      [0]=>
      array(4) {
        ["id"]=>
        string(1) "6"
        ["href"]=>
        string(5) "href6"
        ["content"]=>
        string(13) "subheader 2 1"
        ["parent_id"]=>
        string(1) "2"
      }
    }
  }
  [3]=>
  array(5) {
    ["id"]=>
    string(1) "3"
    ["href"]=>
    string(5) "href3"
    ["content"]=>
    string(7) "header3"
    ["parent_id"]=>
    NULL
    ["submenus"]=>
    array(0) {
    }
  }
}

Output of the menu structure:
header1
  subheader 1 1
  subheader 1 2
header2
  subheader 2 1
header3

Now you can output your all menu items with its submenu items, not only for menu id 3.
