Rizki Wahyu
Rizki Wahyu

Reputation: 118

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

i want result like this in view.

result

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.

Upvotes: 0

Views: 202

Answers (1)

Uttam Kumar Roy
Uttam Kumar Roy

Reputation: 2058

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>

Upvotes: 1

Related Questions