为了提供一些上下文,这是for循环引用的代码:将表单数据放入一个数组中。
$_SESSION['shoppingcart'][$count] = array (
'id' => filter_input(INPUT_GET, 'ID'),
'name' => filter_input(INPUT_POST, 'hidden_name'),
'price' => filter_input(INPUT_POST, 'hidden_price'),
'list_price' => filter_input(INPUT_POST, 'hidden_list_price'),
'quantity' => filter_input(INPUT_POST, 'quantity')
);
$count = count($_SESSION['shoppingcart']);
$product_ids = array_column($_SESSION['shoppingcart'], 'id');
这一切都很好:我理解这段代码的作用。它只是“for”循环,以及在for循环外部涉及变量“$i”的所有内容。
for ($i=0; $i < count($product_ids); $i++) {
if ($product_ids[$i] == filter_input(INPUT_GET, 'ID')) {
$_SESSION['shoppingcart'][$i]['quantity'] +=
filter_input(INPUT_POST,'quantity');
}
}
所以,$i
变量。它在for循环中表示什么,为什么它/在'if‘语句中方括号中表示什么。
我应该说这段代码达到了预期的目的(将表单数据放入数组中,如果数组中的项已经存在,则递增quantity
)。我从一个YouTube教程中得到了代码,但他没有解释$i
部分。
https://stackoverflow.com/questions/51526675
复制相似问题