title: Selectively interating through an multidimentional associative array by their indexes in php
body: <p>I m developing a website using php and I am fetching the data from sql server. What I need to do is there are 2 rows which will contain 3 columns of data each. The associative array I store the data has 6 sub-arrays. I need to create a loop where after 3 entries, a new row is created and the data of next three in printed in next row.  </p>
  
  <p>I tried using while loop but I dont know how I can separate the array by their indexes as doing while($index = mysqli_fetch_assoc(r)) will print all the data but I want 3 to be in one row and 3 in the next. Thanks!</p>
  
  
  
  <pre><code>        &lt;?php
          $q = "SELECT * FROM main_intro";
          $r = mysqli_query($link, $q) or die(mysqli_error($link));
          $num_rows = mysqli_num_rows($r);
          $count = 0;
          $intro = mysqli_fetch_assoc($r);
          $keys = array_keys($intro) ?&gt;
  
          &lt;div class="row custom_pad"&gt;
              &lt;?php
              for ($i = 0; $i &lt; 3; $i++) {
                  ?&gt;
                  &lt;div class="col-md-4"&gt;
                      &lt;h2&gt;&lt;?php echo $intro['image']; ?&gt;&lt;?php echo $intro['heading']; ?&gt;&lt;/h2&gt;
                      &lt;p&gt;&lt;?php echo $intro['data']; ?&gt;&lt;/p&gt;
                  &lt;/div&gt;
              &lt;?php} ?&gt;
              &lt;!-- --&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>What I need is, in first row, i need the first 3 array data and then in next row, i need the other 3.</p>