我有這個多維數組,我想要打印到一個表中,每個記錄/項目進入它自己的行,但它列明智。這是我得到的輸出:http://mypetshopping.com/product.php打印多維數組
ps:$ product的值將根據正在查看的產品進行動態調整。
<?php
session_start();
?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Hash</th>
<th>Quantity</th>
<th>Size</th>
<th>Color</th>
</tr>
</thead>
<tbody>
<?php
function addCart($product, $quantity, $size,$color) {
$hash = md5($product);
$_SESSION['cart'][$product]['name'] = $product;
$_SESSION['cart'][$product]['hash'] = $hash;
$_SESSION['cart'][$product]['quantity'] = $quantity;
$_SESSION['cart'][$product]['size'] = $size;
$_SESSION['cart'][$product]['color'] = $color;
}
addCart('Red Dress',1,'XL','red');
addCart('Blue Dress',1,'XL','blue');
addCart('Slippers',1,'XL','orange');
addCart('Green Hat',1,'XXXL','green');
$cart = $_SESSION['cart'];
foreach($cart as $product => $array) {
foreach($array as $key => $value) {
?>
<tr>
<td><?=$value;?></td>
<td><?=$value;?></td>
<td><?=$value;?></td>
<td><?=$value;?></td>
<td><?=$value;?></td>
</tr>
<?php
}
}
?>