如何将数组拆分为两行表"<tr>"在模板一侧的一个foreach内


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

由于工作环境受限,我需要将这个数组的一半放入一个表行,另一半放入另一个表行。

这是一个数组(已经按照我需要的顺序排序了):

Array
(
    [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
)

我需要将"size_run1" - "size_run10"处的数组拆分为单独的表行。

以前,当我不需要size_run时,我使用这个将数组显示到表中(标准foreach)。由于请求,我正在添加它。(是的,我使用的是过时的php)

<?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>';
     }
  }
?>

我想保持它作为一个数组的原因是,因为我使用一个查询所有的信息,与我正在使用的庞大的数据库,它更有意义的是我分割这在模板方面,而不是有单独的数组,并添加匹配产品到size_run的过程。

在不将数组分割成不同数组的情况下,最好的方法是什么?

我想要的输出,它是将数组'$v'分成两个单独的表,如下所示:

  <tr>
    <th>product</th>
    <th>description</th>
    <th>current_stock</th>
    <th>purchase_order</th>
    <th>total_sales</th>
    <th>avail_to_sell</th>
  </tr>
  <tr>
    <th>size_run1</th>
    <th>size_run2</th>
    <th>size_run3</th>
    <th>size_run4</th>
    <th>size_run5</th>
    <th>size_run6</th>
    <th>size_run7</th>
    <th>size_run8</th>
    <th>size_run9</th>
    <th>size_run10</th>
  </tr>

像这样:

$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>';    
}

我仍然会创建一个新的数组,然后在不需要它的情况下销毁它。

$newArray = array();
foreach($array as $key=>$value){
    $pos = strpos($key, "size_run");
    if($pos!==false){
        $newArray[$key] = $value;
    }
}