dpbv85276 2018-03-07 13:41
浏览 31
已采纳

检查多维数组中是否存在值,并在同一个键内编辑其他值

I am trying to check if a value exists in an array and don't add an entire new entry to it, but instead only add to the quantity in the existing data.

My array looks like this:

Array
(
    [0] => Array
        (
            [product] => Douche 1
            [price] => 1200
            [picture] => cms/images/douche.jpg
            [quantity] => 20
        )

    [1] => Array
        (
            [product] => Douche 1
            [price] => 1200
            [picture] => cms/images/douche.jpg
            [quantity] =>  => 18
        )
)

So to check if a value exists I did the following:

if(in_array('Douche 1', array_column($_SESSION['cart'], 'product'))) { // search value in the array
    echo "FOUND";
}

But instead of echoeing FOUND, I need the array to merge in a way, all stays the same, only the quantity needs to add up.

So when my array is like this:

Array
(
    [0] => Array
        (
            [product] => Douche 1
            [price] => 1200
            [picture] => cms/images/douche.jpg
            [quantity] => 20
        )

)

And I add a product with 15 quantity, I want the array to change to this:

Array
(
    [0] => Array
        (
            [product] => Douche 1
            [price] => 1200
            [picture] => cms/images/douche.jpg
            [quantity] => 35
        )

)

So the quantity adds up only when a productname is added that already exists, if it doesn't exist, there just needs to be a new key (with an array in it).

How can I do that?

My entire array code (excluding ajax and my loop) at the moment is this:

if(isset($_POST['product'])){
  $thisProduct = array(
    'product' => $_POST['product'],
    'price' => $_POST['price'],
    'picture' => $_POST['picture'],
    'quantity' => $_POST['quantity'],
  );
  if (isset($_SESSION['cart'])) {
    $_SESSION['cart'][] = $thisProduct;
  } else {
    //Session is not set, setting session now
    $_SESSION['cart'] = array();
    $_SESSION['cart'][] = $thisProduct;
  }
}

if(in_array('Douche 1', array_column($_SESSION['cart'], 'product'))) { // search value in the array
    echo "FOUND";
}
  • 写回答

1条回答 默认 最新

  • duan_88598 2018-03-07 13:50
    关注

    Instead of trying to change after updating your cart array, you maybe could index your array using the name of the product to check if it exists or not :

    $prod = $thisProduct['product'] ; // shortcut for name
    
    if (!isset($_SESSION['cart'])) {
       $_SESSION['cart'] = [] ;
    }
    
    if (!isset($_SESSION['cart'][$prod])) { // no exists in cart, add it
       $_SESSION['cart'][$prod] = $thisProduct;
    }
    else { // exists increment quantity
       $_SESSION['cart'][$prod]['quantity'] += $thisProduct['quantity'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP