简体   繁体   English

在php中编辑多维数组中的值

[英]editing a value in a multidimentional array in php

I've got a session called Cart_array which holds a multidimensional array in the following way: 我有一个名为Cart_array的会话,该会话以以下方式保存多维数组:

$_SESSION['Cart_array'] = array(
                1 => array(
                    "ID" => $pid,
                    "QTY" => 1
                )
            );

this is how items are added to the cart session. 这是将项目添加到购物车会话的方式。 pid is obtained from another form pid是从另一种形式获得的

if (isset($_POST['pid'])) {
        $pid    = $_POST['pid'];
        if (!isset($_SESSION['Cart_array']) || count($_SESSION['Cart_array']) < 1) { //check if cart session is not set or empty
            $_SESSION['Cart_array'] = array(
                1 => array(
                    "ID" => $pid,
                    "QTY" => 1
                )
            );
        } else {
                array_push($_SESSION['Cart_array'], array(
                    "ID" => $pid,
                    "QTY" => 1
                ));

        } //end else
    } //end if

the user has a form with the following things in a function: 用户具有一个函数中包含以下内容的表单:

     <?php foreach ($_SESSION['Cart_array'] as $eachItem) {
            $itemID = $eachItem['ID'];
            $itemQty = $eachItem['QTY']; >?

        <input class="qty" name="quantity" type="number" value="<?php echo $itemQty;?>" />
        <input type="submit" name="qtyChange<?php echo $itemID;?>" value="Change Qty" />
        <input name="qtyOfItem" type="hidden" value="<?php echo $itemID?>"/> 
}

This form will go through the Cart_array and display the quantity in the cart for every item. 该表格将通过Cart_array并显示每件商品在购物车中的数量。 I want the user to be able to change the quantity in the cart for the specific item that they chose when they click the Change Qty button I'm not sure how to go around doing this? 我希望用户能够在单击“ Change Qty按钮时Change Qty所选商品的购物车中的Change Qty我不确定该怎么做吗?

You could edit the array like this 您可以像这样编辑数组

Your Array 您的阵列

$list = array([0]=>
                   array(
                         [ID]=>'XYZ' 
                         [QTY]=>'1'
                         )
             ); 


    my_function()
   {
    $list=$_SESSION['Cart_array']; 
    global $list;
    $list[0]['QTY'] = '2'; //or this 2 value can be taken from user using jquery 
    }

my_function();

For the script that this submits to, you want to loop through each item in the cart_array session and find it by that ID then change the quantity for that item. 对于此提交的脚本,您要遍历cart_array会话中的每个项目,并通过该ID查找它,然后更改该项目的数量。

 foreach($_SESSION['cart_array'] as $index => $item){
   if($item['ID'] == $_POST['ID']){
     $_SESSION['cart_array'][$index]['quantity'] = $_POST['quantity'];
  }
 }

Just pass your quantity value from the user like this 像这样从用户传递您的数量值

<?php
$_SESSION['Cart_array'] = array(
                1 => array(
                    "ID" => $pid,
                    "QTY" => 1
                )
            );

$_SESSION['Cart_array'][1]['QTY']=30;//Relaces the quantity from 1 to 30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM