PHP两个阵列到一个表中。第一个阵列垂直,第二个水平

时间:2016-03-30 04:12:39

标签: php arrays

我希望看到这样的结果。

result

这是数组的一部分:

Array ( [0] => Array ( [car_id] => ferarri [total] => 15 ) [1] => Array ( [car_id] => lamborgini [total] => 10 ) [2] => Array ( [car_id] => ford [total] => 32 ) [3] => Array ( [car_id] => toyota [total] => 45 ) [4] => Array ( [car_id] => viar [total] => 1 ) ) 1

我该如何看待它。水平显示总数。和垂直显示汽车品牌。

1 个答案:

答案 0 :(得分:1)

您可以使用此

<?php 
$car_tot = array(
  '0' => array ( 
    'car_id' => 'ferarri',
    'total' => 15 
  ), 
  '1' => array (
    'car_id' => 'lamborgini',
    'total' => 10 
  ), 
  '2' => array (
    'car_id' => 'ford',
    'total' => 32 
  ), 
  '3' => array ( 
    'puskesmas_id' => 'toyota',
    'total' => 45 
  ), 
  '4' => array (
    'car_id' => 'viar',
    'total' => 1 
  )
);

echo '<pre>';
print_r( $car_tot );
echo '</pre>';
?>
<table>
<tr>
  <th>Type</th>
  <th>Total</th>
</tr>

<?php 
foreach( $car_tot as $key=>$ctRow )
{
   ?>
  <tr>
    <td>
        <?= !empty( $ctRow['car_id'] ) ? $ctRow['car_id'] : '';?>
    </td>
    <td>
       <?= !empty( $ctRow['total'] ) ? $ctRow['total'] : '';?>
    </td>
  </tr>
  <?php 
}
?>
</table>