請點我在哪裏我錯了我的形式,以及如何獲得訪問$ _ POST數據通過它的關鍵如何通過PHP數組中的鍵訪問值?
這裏是我的表單輸入:
<input type="hidden" name="<?php echo $products_in_cart; ?>['product_name']" value="<?php echo $_SESSION['cart']['products'][$val['sku']]['product_name']; ?>">
<input type="hidden" name="<?php echo $products_in_cart; ?>['qty_in_cart']" value="<?php echo $_SESSION['cart']['products'][$val['sku']][$qty]; ?>">
<input type="hidden" name="<?php echo $products_in_cart; ?>['price_for_item']" value="<?php echo $val['price_item']; ?>">
<input type="hidden" name="<?php echo $products_in_cart; ?>['price_for_all_items_in_cart']" value="<?php echo $price_item_total; ?>">
捕獲數據:
$products_in_cart = $_POST['products_in_cart'];
for ($i=0; $i < count($products_in_cart); $i++) {
echo "<pre>";
print_r($products_in_cart[$i]);
echo "</pre>";
// !!! ISSUE: Can't access by this key
// echo $products_in_cart[$i]['product_name'] . "<br>";
}
陣列:
> [products_in_cart] => Array
> (
> [0] => Array
> (
> ['product_name'] => Котёл на отработанном масле «EcoBoil-18/30»
> ['qty_in_cart'] => 1
> ['price_for_item'] => 35200
> ['price_for_all_items_in_cart'] => 35200
> )
>
> [1] => Array
> (
> ['product_name'] => Калорифер воздушный «HotAir-2/36»
> ['qty_in_cart'] => 2
> ['price_for_item'] => 48000
> ['price_for_all_items_in_cart'] => 96000
> )
>
> )
UPD 這樣的工作,但不能findout IA爲什麼不能獲得通過的關鍵
foreach ($products_in_cart as $product) {
foreach ($product as $p) {
echo $p . "<br>";
// This not works:
// echo $p['product_name'] . "<br>";
}
}
你能迴應類似'$ products_in_cart [1] ['price_for_item']'嗎? – Michel