Q:

How can I group a foreach loop's output?

I'm working with Bootstrap's grid and I'm iterating through an array of names. The problem is that I don't know how to place X amount of results per parent div. 
$names = mysql result containing 9 names.
<div class="container">
  <div class="row">
    <?php
    foreach ($names as $name){
     echo '
      <div class="col-xs-3 col-sm-3">
        <p>Customer name:'.$name.'</p>
      </div>';  
     }  
   </div>
</div>

That would output this 9 times.
<div class="col-xs-3 col-sm-3">
  <p> Customer name: Foo </p>
</div>
<div class="col-xs-3 col-sm-3">
  <p> Customer name: bob </p>
</div> 
<div class="col-xs-3 col-sm-3">
  <p> Customer name: jim </p>
</div> 
<div class="col-xs-3 col-sm-3">
  <p> Customer name: dave </p>
</div> 
<div class="col-xs-3 col-sm-3">
  <p> Customer name: lucy </p>
</div> 
<div class="col-xs-3 col-sm-3">
  <p> Customer name: sarah </p>
</div> 
<div class="col-xs-3 col-sm-3">
  <p> Customer name: geoff </p>
</div> 
<div class="col-xs-3 col-sm-3">
  <p> Customer name: matt </p>
</div> 
<div class="col-xs-3 col-sm-3">
  <p> Customer name: alex </p>
</div>  

How can I make it output 3 column divs per .row like this?
<div class="row">
    <div class="col-xs-3 col-sm-3">
      <p> Customer name: Foo </p>
    </div>
    <div class="col-xs-3 col-sm-3">
      <p> Customer name: bob </p>
    </div> 
    <div class="col-xs-3 col-sm-3">
      <p> Customer name: jim </p>
    </div>
</div>

<div class="row">
    <div class="col-xs-3 col-sm-3">
      <p> Customer name: dave </p>
    </div> 
    <div class="col-xs-3 col-sm-3">
      <p> Customer name: lucy </p>
    </div> 
    <div class="col-xs-3 col-sm-3">
      <p> Customer name: sarah </p>
    </div> 
</div>

<div class="row">
    <div class="col-xs-3 col-sm-3">
      <p> Customer name: geoff </p>
    </div> 
    <div class="col-xs-3 col-sm-3">
      <p> Customer name: matt </p>
    </div> 
    <div class="col-xs-3 col-sm-3">
      <p> Customer name: alex </p>
    </div>  
</div>

A:

You may want to think about an array_chunk() which would take care of uneven number of names:
$names  =   array('Dave','Frank','Sarah','Dan','Andrew','Jessica','Alena','Debbie','Jeff');
$split  =   array_chunk($names,3);
?>
<div class="container">
<?php
    foreach ($split as $group){
        echo '
    <div class="row">';
        foreach ($group as $name){
            echo '
        <div class="col-xs-3 col-sm-3">
            <p>Customer name:'.$name.'</p>
        </div>';
        }
        echo '
    </div>';
    } 
?>
</div>

