dongyi2006 2017-04-04 14:20
浏览 194
已采纳

PHP:如何更新数组内的数组中的数据

Following is my PHP code:

while($row = $resp->fetch_assoc())
{
    $itemArray = array(
      array(
        'name' => $row["product_name"],
        'id' => $row["id"],
        'image' => $row['images'],
        'discount' => $row["discount"],
        'quantity' => $min_quantity,
        'price' => $row["price"]
      )
    );
}

if(!empty($_SESSION["cart_item"]))
{
    if(in_array($itemArray, $_SESSION["cart_item"]))
    {
        foreach($_SESSION["cart_item"] as $item)
        {
            if(in_array($item, $itemArray))
                $_SESSION["cart_item"][$item]["quantity"] = $min_quantity;
            break;
        }
    }
    else
    {
        $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
    }
}
else
{
    $_SESSION["cart_item"] = $itemArray;
}

After executing this code i am getting a response like this:

Array
(
[0] => Array
    (
        [name] => Girls Designer Dress
        [id] => 4
        [image] => s:146:"2017/march/meetfashion01-04-2017_12-19-07_am.jpg,2017/march/meetfashion01-04-2017_12-19-08_am
.jpg,2017/march/meetfashion01-04-2017_12-19-09_am.jpg";
        [discount] => 10
        [quantity] => 1
        [price] => 1200
    )

)

What I am trying to acheive is if user adds the same product in cart whose data we have got in response then instead of creating once more array and merging it one after another i want to just update the quantity of the same product from 1 to 2.

I am stuck in this part of code

foreach($_SESSION["cart_item"] as $item)
{
    if(in_array($item, $itemArray))
    $_SESSION["cart_item"][$item]["quantity"] = $min_quantity;
    break;
}

I have tried it many times that if same product is encountered then increment the quantity by 1 for the same product and don't create one more array.

Can anyone help with this logic and code?

  • 写回答

2条回答 默认 最新

  • dtyrxmoj20617 2017-04-04 15:14
    关注

    is $_SESSION["cart-item"] a single Array like?

    Array
    (
        [name] => Girls Designer Dress
        [id] => 4
        [image] => s:146:"2017/march/meetfashion01-04-2017_12-19-07_am.jpg,2017/march/meetfashion01-04-2017_12-19-08_am
    .jpg,2017/march/meetfashion01-04-2017_12-19-09_am.jpg";
        [discount] => 10
        [quantity] => 1
        [price] => 1200
    )
    

    If so then don't call a foreach instead just check directly if that cart-item is in the $itemArray using array_search().

    (The function search in a array and if needle is found return its index else return false).

    $cartItemPosition = array_search($_SESSION['cart-item'], $itemArray);
    
    if($cartItemPosition == false) {
      // Not found in array, so create new entry
      $itemArray.push($_SESSION['cart-item']);
    
    }else {
      // Found in array so edit existing entry
    
        $itemArray[$cartItemPosition]['quantity'] = 999;  
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答