【问题标题】:Edit the data of the array inside of the loop - PHP在循环内编辑数组的数据 - PHP
【发布时间】:2021-02-09 23:31:07
【问题描述】:

我正在尝试使用会话和循环编辑数组的数据。

此代码中的预期输出是,在循环内编辑特定数据,但问题是正在编辑其他数据。请帮我解决这个问题,谢谢。

注意:如果$cart_actionadd,则会添加新数据,但如果$values['type'] 等于$_POST['type'],则会编辑产品的具体数量。

例如,我想编辑product_id MQFUQ的数量,但我不知道如何编辑这个,product_id SHAREZ总是被编辑。 //数据 大批 ( [0] => 数组 ( [产品编号] => NP1DC [product_id] => 28AHT [类型] => SOLOZ [数量] => 1 )

    [1] => Array
        (
            [product_number] => DZRW8
            [product_id] => 28AHT
            [type] => SHAREZ
            [qty] => 1
        )

    [2] => Array
        (
            [product_number] => 4G0RY
            [product_id] => MQFUQ
            [type] => 
            [qty] => 2
        )

    [3] => Array
        (
            [product_number] => ZS1C6
            [product_id] => 28AHT
            [type] => SOLOZ
            [qty] => 1
        )

)

//here's my code
$result = 0;
$values_type = "";
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
    $values_type = $values['type'];
    if($values["product_id"] == $_POST["product_id"] && $values['type'] == $_POST['type'])
    {
        $item_array = array(
            'product_number'            =>  $values['product_number'],
            'product_id'            =>  $_POST["product_id"],
            'type'          =>  $_POST["type"],
            'qty'       =>  $_POST['qty'] + $values['qty']
        );
        $_SESSION["shopping_cart"][$keys] = $item_array;
        $result = $keys;
        $cart_action = "edit";
    }
    else {
        $values_type = $values['type'];
        $count = count($_SESSION["shopping_cart"]);
        $item_array = array(
            'product_number'            =>  $product_number,
            'product_id'            =>  $_POST["product_id"],
            'type'          =>  $_POST["type"],
            'qty'       =>  $_POST['qty']
        );
        $result = $count;
        $cart_action = "add";
    }
}

if ($cart_action == "add") {
    $_SESSION["shopping_cart"][$result] = $item_array;
    echo $cart_action ." ". $values_type;
}
elseif ($cart_action == "edit") {
    $_SESSION["shopping_cart"][$result] = $item_array;
    echo $cart_action ." ". $values_type;
}
else {
    echo "";
}

【问题讨论】:

  • 如果您显示原始数组(代表性样本var_export())和目标数组会有所帮助。
  • @jibsteroos 您好,我已经编辑了我的帖子,请检查您是否有时间,谢谢先生。
  • 首先 - 如果在这里中继您的代码,产品将始终被添加,而不是计算在内。您根据会话检查两件事 - 类型和 product_id。但是鉴于 var_dump 说类型总是不同的。其次 - 为什么让它这么难?检查会话项目的产品ID,如果有,会话项目数量+1,否则添加新产品

标签: php arrays session


【解决方案1】:
$values_type = "";
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
    $values_type = $values['type'];
    if($values["product_id"] == $_POST["product_id"] && $values['type'] == $_POST['type'])
    {
        $item_array = array(
            'product_number'            =>  $values['product_number'],
            'product_id'            =>  $_POST["product_id"],
            'type'          =>  $_POST["type"],
            'qty'       =>  $_POST['qty'] + $values['qty']
        );
        $_SESSION["shopping_cart"][$keys] = $item_array;
        $result = $keys;
        $cart_action = "edit";
    }  else {
        $values_type = $values['type'];
        $count = count($_SESSION["shopping_cart"]);
        $item_array = array(
            'product_number'            =>  $product_number,
            'product_id'            =>  $_POST["product_id"],
            'type'          =>  $_POST["type"],
            'qty'       =>  $_POST['qty']
        );
        array_push($_SESSION["shopping_cart"], $item_array); //adds the item array as a new element in the shopping_cart array
        $result = $count;
        $cart_action = "add";
    }
}

我希望这能解决您的问题。问题是您在循环内设置了$result$cart_action。这些值随着循环的每次迭代而改变,因此您的值可能与循环后的预期不同,这些值将仅由最后一次迭代确定。

【讨论】:

  • 您好,谢谢先生,但是如果我正在编辑值,问题仍然存在,系统正在添加一个新数组,例如我在购物车中添加了一个产品,然后如果我点击添加再次购物车,将添加相同的产品而不是编辑值。
  • 问题是,例如,如果数组是 3,并且我将 if else 语句放在循环内,则显示 3 product_id 而不是仅显示 1 product_id。
【解决方案2】:

如果类型不是那么重要,那么可能是这样的吗?

<?php
$result = 0;
$values_type = "";
foreach($_SESSION["shopping_cart"] as $keys => $values) {
    $values_type = $values['type'];
    if($values["product_id"] == $_POST["product_id"]) // remove that! && $values['type'] == $_POST['type'])
    {
/*
        $item_array = array(
            'product_number'            =>  $values['product_number'],
            'product_id'            =>  $_POST["product_id"],
            'type'          =>  $_POST["type"],
            'qty'       =>  $_POST['qty'] + $values['qty']
        );
*/
        $_SESSION["shopping_cart"][$keys]["qty"] += $_POST['qty'];
        $cart_action = "edit";
    }
    else {
        $values_type = $values['type'];
        $count = count($_SESSION["shopping_cart"]);
        $_SESSION["shopping_cart"][$count] = array(
            'product_number'            =>  $product_number,
            'product_id'            =>  $_POST["product_id"],
            'type'          =>  $_POST["type"],
            'qty'       =>  $_POST['qty']
        );
        $result = $count;
        $cart_action = "add";
    }
}
?>

更新

另外,还有一件事要考虑 - 如果类型是某种分隔符(如颜色或尺寸),那么它实际上是购物车中的不同产品(项目),你怎么知道以及需要多少。 此外 - 您应该能够在不使用它的情况下编辑会话 - 就像在 foreach 中使用 & 运算符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 2012-01-25
    • 2018-01-21
    相关资源
    最近更新 更多