简体   繁体   English

PHP将两个数组合并为一个表。 第一阵列垂直,第二水平

[英]PHP Two Arrays Into One Table. First Array Vertical, Second Horizontal

i want result like this in view. 我想要这样的结果。

结果

This is (part of) the array: 这是数组(的一部分):

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

how do I make it look. 我如何使它看起来。 horizontally displays the total. 水平显示总计。 and vertical displays car brand. 和垂直展示汽车品牌。

You can use this 你可以用这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将两个数组合并到一个表中。 第一列垂直,第二列水平 - Two arrays into one table. First array vertical, second horizontal PHP 将两个数组合并到一个表中。 第一个阵列垂直和水平 - PHP Two Arrays Into One Table. First Array Vertical & Horizontal 两个数组,一个数组列出项目,第二个对第一个进行排序,php代码如何? - Two arrays, one array to list items, second to sort the first one, php code how to? 合并两个数组,用第二个数组覆盖第一个数组 - Merging two arrays, overwriting first array with second one PHP 比较两个 arrays 但第一个数组的所有值都需要在第二个 - PHP compare two arrays but all values of first array needs to be in second PHP使用两个数组:第一个按值,第二个按键(基于第一个数组顺序) - PHP usort two arrays: first by value, second by key based on first array order 首先根据值合并两个数组,然后根据key-php合并第二个数组 - Merge two arrays first one based on value and second one based on key - php 一个数组中的两个数组PHP - Two arrays in one array PHP 如何通过仅接管与第一个具有相同键的第二个数组中的值来合并两个 arrays? - How to merge two arrays by taking over only values from the second array that has the same keys as the first one? 使用PHP的水平表而不是垂直表 - Horizontal table instead of vertical with PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM