0
<table style="width: 600px" class="slicedTable">
     <tr>
      <th>Spetsialist</th>
      <th>Tunnid</th>
     </tr>


<tr>            
       <?php foreach($specs as $specName => $spec): ?>
        <td><?php echo $specName?>
        <tr>
       <?php endforeach; ?>


<td>
        <?php foreach($tunnid as $tund): ?>
           <td><?php echo $tund?></td>

       </td>
       <?php endforeach; ?>
     </tr>
 </table>

我需要这个来打印出一个常规表,它的位置如下:
name 1 - value1
name2 - value2

眼镜:

$specs = array();

            foreach($data as $row) {
                $workerName = $row['Worker']['name'];
                if (!isset($specs[$workerName])) {
                    $specs[$workerName] = array('procs' => array(), 'extras' => array());
                }

但是我似乎无法做到这一点,请帮助。

4

2 回答 2

1

这是我为您修复的代码:

假设 中的元素数$spec等于或大于 中的元素数$tunnid。在编码员的角度,count($spec) >= count($tunnid).

<table style="width: 600px" class="slicedTable">
   <tr>
      <th>Spetsialist</th>
      <th>Tunnid</th>
   </tr>
   <?php 
      $i = 0;
      foreach($specs as $specName => $spec): 
    ?>
   <tr>            
       <td><?php echo $specName; ?></td>
       <td><?php echo isset($tunnid[$i]) ? $tunnid[$i] : '-'; ?></td>
    </tr>
    <?php 
         $i++;
      endforeach; 
    ?>
 </table>

我承认,这并不是一个优雅的解决方案。

于 2013-06-19T07:40:51.623 回答
0

更新
似乎你的数组可能有不同的长度,所以我更新了下面的代码来处理这种情况(哪个数组更长并不重要)。


你可以这样做:

<table style="width: 600px" class="slicedTable">
    <tr>
        <th>Spetsialist</th>
        <th>Tunnid</th>
    </tr>
<?php $len1 = count($specs); $len2 = count($tunnid);
      $specsKeys = array_keys($specs);
      for ($i = 0; $i < max($len1, $len2); $i++) {
          $name  = ($i < $len1) ? $specsKeys[$i] : ""; 
          $value = ($i < $len2) ? $tunnid[$i] : ""; ?>
    <tr>
        <td><?php echo($name); ?></td>
        <td><?php echo($value); ?></td>
    </tr>
<?php } ?>
</table>

另请参阅这个简短的演示(*更新*)。

于 2013-06-19T07:45:36.293 回答