Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(PHP)在for循环中处理变量_Php_Arrays_For Loop - Fatal编程技术网

(PHP)在for循环中处理变量

(PHP)在for循环中处理变量,php,arrays,for-loop,Php,Arrays,For Loop,为了提供一些上下文,这是for循环引用的代码:将表单数据放入数组 $_SESSION['shoppingcart'][$count] = array ( 'id' => filter_input(INPUT_GET, 'ID'), 'name' => filter_input(INPUT_POST, 'hidden_name'), 'price' => filter_input(INPUT_POST, 'hid

为了提供一些上下文,这是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'语句的方括号中表示什么

我应该说,这段代码实现了它的预期目的,将表单数据放入数组,如果数组中的项已经存在,则增加数量。
我从YouTube教程中获得了代码,只是他没有解释$I部分。

每次循环完成后,$I变量将增加$I+,因此在本例中增加1,直到满足以下条件:$I
还值得注意的是,$i=0是$i的初始化。这只在您第一次输入for循环时发生一次。

在发布之前,您是否查阅了手册?我投票将此问题作为离题回答,因为手册中有关于for循环如何工作的良好文档。请不要从web下载代码,在您不理解时使用它。这样做只会让你的应用程序充满漏洞和安全漏洞,而且,当你的服务器被劫持时,会让互联网对每个人都更加危险。此外,for循环是绝大多数语言中最基本的编程概念之一,当然是所有“类似C”的语言如果您不知道for循环是如何工作的,我给您的最好建议是,在做任何其他事情之前,先花时间阅读一些基本教程。试图在不理解循环的情况下学习编程只会给你带来很大的挫折。同意@EdCottrell关于安全性的观点。例如,filter\u inputINPUT\u GET,“ID”看起来可能会过滤某些内容,但默认过滤器是filter\u UNSAFE\u RAW。