title: Fill an array with multiple arrays with an foreach loop in php
body: <p>I have a problem which I cant figure out. I have an array $navItems. In there I have multiple arrays with the items.</p>
  <p><a href="https://i.stack.imgur.com/UI08N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UI08N.png" alt="print_r($navItems)" /></a></p>
  <pre class="lang-php prettyprint-override"><code>$navItems = array(
      array(
          &quot;url&quot; =&gt; 'index.php',
          &quot;title&quot; =&gt; 'Home',
          &quot;id&quot; =&gt; 1
      ),
      array(
          &quot;url&quot; =&gt; &quot;pages/projects.php&quot;,
          &quot;title&quot; =&gt; &quot;Projects&quot;,
          &quot;id&quot; =&gt; 2
      ),
      array(
          &quot;url&quot; =&gt; &quot;pages/about.php&quot;,
          &quot;title&quot; =&gt; &quot;About&quot;,
          &quot;id&quot; =&gt; 3
      ),
      array(
          &quot;url&quot; =&gt; &quot;pages/contact.php&quot;,
          &quot;title&quot; =&gt; &quot;Contact&quot;,
          &quot;id&quot; =&gt; 4
      ),
      array(
          &quot;url&quot; =&gt; &quot;pages/login.php&quot;,
          &quot;title&quot; =&gt; &quot;Login&quot;,
          &quot;id&quot; =&gt; 5
      ),
  );
  </code></pre>
  <pre class="lang-php prettyprint-override"><code>// Database conectie en query
  $dbh = getDbConnection();
  $sql = &quot;SELECT * FROM pages&quot;;
  $pages = $dbh-&gt;query($sql);
  
  
  // Navitems 
  $navItems = array();
  
  foreach($pages as $singe_page){
      $page_title = $singe_page['title'];
      $page_url =$singe_page['url'];
      $page_id = $singe_page['id'];
      
      $pagina = array(
          'title' =&gt; $page_title,
          'url' =&gt; $page_url,
          'id' =&gt; $page_id
      );
  
      array_push($navItems, $pagina);
  }
  //This is the code on my array.php page. And it loads that in on the head.php
  </code></pre>
  <p>But now I want to add all the page arrays with an foreach loop. Because I have the pages saved in my database. And now I'm trying to pull the pages out my database and put them in the array.</p>
  <p>But I just don't know anymore...</p>
  <p>I just want for every record in my database it to throw that in an array and that array in the $navItems array.</p>
  <p>Nevermind. When I was typing this I changed a little bit of code. And now it works. I added the <code>array_push($navItems, $pagina)</code> in the foreach loop. And it works.</p>