0

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

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

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>
4

2 回答 2

1

像这样的东西:

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

}

如果您想更改密钥,仍然可以维护。

于 2013-05-14T23:19:02.797 回答
0

如果您不需要它,我仍然会创建一个新数组,然后将其销毁。

$newArray = array();

foreach($array as $key=>$value){
    $pos = strpos($key, "size_run");
    if($pos!==false){
        $newArray[$key] = $value;
    }
}
于 2013-05-14T23:04:33.357 回答