首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >(PHP)处理for循环中的变量

(PHP)处理for循环中的变量
EN

Stack Overflow用户
提问于 2018-07-26 03:51:44
回答 1查看 35关注 0票数 -2

为了提供一些上下文,这是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部分。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51526675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档