【问题标题】:Updating value in array updates all values with the last inserted value PHP更新数组中的值会使用最后插入的值 PHP 更新所有值
【发布时间】:2020-09-14 07:01:43
【问题描述】:

我有两个数组,一个包含名为 aantal 的数量,另一个包含名为 producten 的产品。

产品:

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => Tegel zwart
            [2] => Zwarte tegel 2x2m
            [3] => 47,5
            [4] => 25
        )

    [1] => Array
        (
            [0] => 4
            [1] => Tegel lichtgrijs
            [2] => Lichtgrijze tegel 2x2m
            [3] => 40,5
            [4] => 25
        )

    [2] => Array
        (
            [0] => 2
            [1] => Tegel zwart
            [2] => Zwarte tegel 2x2m
            [3] => 47,5
            [4] => 25
        )

    [3] => Array
        (
            [0] => 4
            [1] => Tegel lichtgrijs
            [2] => Lichtgrijze tegel 2x2m
            [3] => 40,5
            [4] => 25
        )

)

安塔尔:

Array
(
    [0] => 20
    [1] => 20
    [2] => 27
    [3] => 25
)

我想根据 url 中的 $_GET 值使用以下代码更新每个数量:

$sum = 0;
foreach($_SESSION['producten'] as $key => $product){
    //$_SESSION['producten'][$key][4] = '';
    $number = str_replace(',', '.', $product[3]);
    $sum+= $number;
    if(!empty($_GET['aantal'])){
        foreach($_GET['aantal'] as $keyaantal => $aantal){
            $_SESSION['producten'][$key][4] = $_GET['aantal'][$keyaantal];
        }
    }else{
        $_SESSION['producten'][$key][4] = '1';
    }
}

这是带有 html 的表单:

<form action="cart.php">
<?php foreach ( $_SESSION['producten'] as $row){

    if(!empty($_GET['aantal'])){
        $aantalwaarde = $row[4];
    }else{
        $aantalwaarde = 1;
    }

    // if($row != ){

    // }
    ?>


        <tr>
            <td><?php echo $row[0] // ID; ?></td>
            <td><?php echo $row[1] // Product_naam; ?></td>
            <td><?php echo $row[2] // Beschrijving; ?></td> 
            <td><input name="aantal[]" type="number" value="<?PHP echo $aantalwaarde; ?>"></td> 
            <td>€<?php echo $row[3] // Prijs; ?></td>   

            <?php $total = $total + intval($row[3]); ?>
        </tr>

        <?php } ?>

        <tr>
            <td></td>
            <td></td>
            <td></td> 
            <td></td>   
        </tr>
        <tr>
            <td><input type="submit" value="Updaten"></td>
            <td></td>
            <td>Totaalprijs</td>
            <td></td> 
            <td>€<?php echo $sum // Prijs; ?></td>   

        </tr>

</form>

为什么如果我单击更新按钮,它会更新所有产品的最后插入值?例如,如果我将最后输入的数量设置为 25,将之前的数量设置为 10,则所有产品的数量都为 25。

我该如何解决这个问题?

【问题讨论】:

  • 如前所述,您忽略了这些是数组的事实。奇怪的事情可能会在一个循环中发生。在关键点添加一些调试代码以检查您正在使用的值,它们通常不是您认为您正在使用的值。

标签: php arrays session


【解决方案1】:

你的代码有很大的缺陷。首先,您遍历整个 producten 数组。在此数组中,您循环遍历 aantal 数组并更新值。这真的没有意义,你根本不需要第二个循环。您可以替换整个部分...

foreach($_GET['aantal'] as $keyaantal => $aantal){
    $_SESSION['producten'][$key][4] = $_GET['aantal'][$keyaantal];
}

...用这个...

$_SESSION['producten'][$key][4] = $_GET['aantal'][$key];

这应该可以解决问题。

您的循环的问题在于您遍历了数组并在每次迭代时更新了['producten'][$key][4] 的值。这意味着您坚持的最后一个值始终是最后一个。

【讨论】:

    【解决方案2】:

    您的内部 foreach 循环是问题所在。您正在检查所有数量,而不是选择相关数量。因此,当循环结束时,您总是会留下最后一个添加到会话中的内容。

    我现在无法测试,但我认为你应该改变

    foreach($_GET['aantal'] as $keyaantal => $aantal){ 
       $_SESSION['producten'][$key][4] = $_GET['aantal'][$keyaantal]; 
    } 
    

    $_SESSION['producten'][$key][4] = $_GET['aantal'][$key]; 
    

    这样,您可以为两个数组选择匹配的键。

    【讨论】:

      猜你喜欢
      • 2018-09-20
      • 2016-10-08
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      相关资源
      最近更新 更多