Andrew Chen Andrew Chen - 1 year ago 59
HTML Question

How to split an array into two table rows "<tr>" within one foreach on template side

Due to working with a restricted working environment, I need to put half of this array into one table row and another half into another .

This is the array (already sorted in order of what I need):

[product] => KFE-1221
[description] => Classic Blue
[current_stock] => 630
[purchase_order] =>
[total_sales] => 0
[avail_to_sell] => 630
[size_run1] => 15
[size_run2] => 62
[size_run3] => 122
[size_run4] => 113
[size_run5] => 102
[size_run6] => 92
[size_run7] => 63
[size_run8] => 61
[size_run9] => 0
[size_run10] => 0

I need to split the array at "size_run1" - "size_run10" into a separate table row.

Previously, I had this to display the array into a table (standard foreach) when I didn't need the size_run. Due to request I am adding it in. (And yes I am using deprecated php)

while($data = mssql_fetch_assoc($result_ats)) {
if ($data['avail_to_sell'] <= 0){
echo '<tr class="error">';
else {
echo '<tr>';
foreach($data as $k => $v){

echo '<td>'.$v.'</td>';
echo '</tr>';


The reason I want to keep it as one array is because I am using one query for all the information, and with the huge database that I'm working with, it makes more sense for me to split this up on template side rather than have separate arrays and add a process of matching the product to the size_run.

What is the best way of doing this without having the split the array up into different arrays?

My desired output, which is to split up array '$v' into two separate tables like below:


Answer Source

Something like this:

$firstRow = array('product', 'description', 'current_stock', 'purchase_order', ... );
$secondRow = array('size_run1', 'size_run2', 'size_run3', ... );

while($data = mssql_fetch_assoc($result_ats)) {

  echo '<tr>';

  foreach($firstRow as $key){

    echo '<td>' . $data[$key] . '</td>';


  echo '</tr>';  
  echo '<tr>';

  foreach($secondRow as $key){

    echo '<td>' . $data[$key] . '</td>';


  echo '</tr>';    


Still maintainable if you want to change the keys.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download