dongwei4096 2013-10-20 03:41
浏览 23

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

I've got a session called Cart_array which holds a multidimensional array in the following way:

$_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

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. 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?

  • 写回答

3条回答 默认 最新

  • doukuiqian5345 2013-10-20 03:53
    关注

    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();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数