繁体   English   中英

更新多维数组(PHP)中的特定键

[英]Updating a specific key within a multidimensional array (PHP)

我在更新我创建的多维数组中的数量值时遇到了一些困难,希望您能帮助我纠正出错的地方。

的背景

我有两个“项目”,它们都有一个简单的表单标签,后跟一个具有唯一值的隐藏输入字段(第一项为1,第二项为2)。 使用POST方法,该按钮将仅指向该页面。

然后,页面右侧的div将加载一个“篮子”,该篮子将使用这些发布值并将其添加到数组中。

当再次使用“添加”按钮时,该值应更新为+1,而不是创建另一个sub_array。

目前正在发生什么

当前,当我第一次单击“添加”时,它会按预期添加数组。

结果1

但是,当第二次单击“添加”时,它将添加第二个数组,而不是数量上的+1'。

结果2

第三次单击“添加”,实际上它现在确实找到了原始值并按我的预期进行了更新,如果我再次单击,它将继续更新数量

结果3

好像是第二次我单击“添加”。

剧本

<?php session_start();

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

if (ISSET($_POST["prod"]))
{
    if(in_array($_POST["prod"],$_SESSION["cart"])==TRUE)
    {
      $_SESSION["cart"][0] = 
      array($_POST["prod"],$_POST["name"],$_SESSION["cart"][0][2]+1);
    }
    else{
         echo 'running else';
         $_SESSION["cart"]=array($_POST["prod"],$_POST["name"],1);}}

         if ($_POST['e']=='1')
         {
           $_SESSION['cart'] = '';
         }
        echo '<br /><br />';
print_r($_SESSION["cart"]);
}

样本表格

<form action="test.php" method="post" enctype="application/x-www-form-urlencoded">
MAST-O-MIR<br/>
img<br/>
£2.00<br/>
        <input type="hidden" value="1" name="prod" />
        <input type="hidden" value="MAST-O-MIR" name="name" />
    <button class="plus-btn" type="Submit">Add</button>
</form>

另外,您可能会从我的脚本中注意到的是,当您“添加”第二项时,它实际上将通过从头开始创建数组来覆盖第一项,因此,如果您可以帮助我解决这两项问题,我将不胜感激专业知识!

非常感谢所有人!

我尝试调试您的代码,可能的解决方案如下:

<?php

 session_start();

if(!isset($_SESSION["cart"]))
{
    $_SESSION["cart"]=[];
}

if (isset($_POST["prod"]))
{
    $prod_id=$_POST["prod"];
    //let suppose $_POST['prod'] is your item id
    $found=false;
    for($i=0;$i<count($_SESSION['cart']);$i++)
    {
        if(isset($_SESSION['cart'][$prod_id]))
        {
            echo "found! so add +1";
            $_SESSION['cart'][$prod_id][2]+=1;
            $found=true;
            break;
        }
    }
    if($found==false)
    {
        echo 'not found! so create a new item';
        $_SESSION["cart"][$prod_id]=array($_POST["prod"],$_POST["name"],1);
    }
}

         if (isset($_POST['e']) && $_POST['e']=='1')
         {
             $_SESSION['cart'] = '';
         }

        echo '<br /><br />';
print_r($_SESSION["cart"]);

?>
<form action="cart.php" method="post" enctype="application/x-www-form-urlencoded">
    MAST-O-MIR<br/>
    img<br/>
    £2.00<br/>
    <input type="hidden" value="1" name="prod" />
    <input type="hidden" value="MAST-O-MIR" name="name" />
    <button class="plus-btn" type="Submit">Add</button>
</form>
<form action="cart.php" method="post" enctype="application/x-www-form-urlencoded">
    MAST-O-MIR<br/>
    img<br/>
    £2.00<br/>
    <input type="hidden" value="2" name="prod" />
    <input type="hidden" value="MAST-O-MIR" name="name" />
    <button class="plus-btn" type="Submit">Add</button>
</form>

另一种方法是使用关联数组。 下面的代码在$ _SESSION中创建一个购物车数组,使用商品名称作为键(因此,您无需遍历购物车数组即可找到该商品)以及每个商品的属性为name => value的数组。

session_start();

if(!isset($_SESSION["cart"]))
{
    $_SESSION["cart"]=[];
}
//let's suppose you have unique names for items
if (isset($_POST["prod"]))
{
    $name=$_POST["name"];
    if(isset($_SESSION['cart'][$name]))
    {
        echo "found! so add +1";
        $_SESSION['cart'][$name]['quantity']+=1;
    }
    else
    {
       echo 'not found! so create a new item';
       $_SESSION["cart"][$name]=array("id"=>$_POST["prod"],"name"=>$_POST["name"],"quantity"=>1);
    }
}

         if (isset($_POST['e']) && $_POST['e']=='1')
         {
             $_SESSION['cart'] =[];
         }

        echo '<br /><br />';
print_r($_SESSION["cart"]);

?>
<form action="cart2.php" method="post" enctype="application/x-www-form-urlencoded">
    MAST-O-MIR<br/>
    img<br/>
    £2.00<br/>
    <input type="hidden" value="1" name="prod" />
    <input type="hidden" value="MAST-O-MIR" name="name" />
    <button class="plus-btn" type="Submit">Add</button>
</form>
<form action="cart2.php" method="post" enctype="application/x-www-form-urlencoded">
    MAST-O-MIR<br/>
    img<br/>
    £2.00<br/>
    <input type="hidden" value="2" name="prod" />
    <input type="hidden" value="MAST-OMIR" name="name" />
    <button class="plus-btn" type="Submit">Add</button>
</form>

没有示例表单就很难测试代码,但是我想您的两个问题都可以通过替换来解决:

$_SESSION["cart"][0] = array($_POST["prod"], $_POST["name"], $_SESSION["cart"][0][2]+1);

对于:

$_SESSION["cart"][0][2]+= 1;

顺便说一句,尝试在要发布代码时适当缩进代码。 很难读。

暂无
暂无

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

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