dpjhq00684 2018-03-15 15:24
浏览 42
已采纳

合并对象和数组

I am posting some data in json format to a php script, this data needs to replace existing data that is in a session (array).

This is what I post to my PHP script:

Array
(
    [0] => stdClass Object
        (
            [product] => Bad 1
            [quantity] => 2
        )

    [1] => stdClass Object
        (
            [product] => Bad 14
            [quantity] => 1
        )

)

And this is my session array ($_SESSION['cart']):

Array
(
    [Bad 1] => Array
        (
            [artikelid] => 2
            [product] => Bad 1
            [price] => 1000
            [picture] => cms/images/bad.jpg
            [quantity] => 2
            [alias] => bad1
            [catalias] => baden
        )

    [Bad 14] => Array
        (
            [artikelid] => 11
            [product] => Bad 14
            [price] => 800
            [picture] => images/defaultimage.jpg
            [quantity] => 1
            [alias] => bad-14
            [catalias] => baden
        )

)

I want the posted quantity value to replace the quantity value inside the session array, but only for the correct product ofcourse. So if I post quantity '10' with product 'bad 1' only the quantity of key 'bad 1' needs to be replaced with that value.

This also goes when posting multiple products like in my example (the posted object).

How can I change the object into an array and merge (replace quantity only) it with my session array?

Something similar I tried but this only works for one posted value.

//Wanneer er een post waarde is vanaf het ajax script:
if($_POST['product']){
    //Stop de productnaam in de variabele $prod
    $prod = $thisProduct['product'] ;
    //Als er nog geen sessie bestaat, maak deze dan aan
    if (!isset($_SESSION['cart'])) {
         //en maak er gelijk een array van
       $_SESSION['cart'] = [];
    }
    //Als de productnaam nog niet voorkomt in de sessie, voeg deze dan toe inclusief de overige array waarden
    if (!isset($_SESSION['cart'][$prod])) {
       $_SESSION['cart'][$prod] = $thisProduct;
    }
    //Als deze wel voorkomt voeg hem dan niet toe maar tel de quantity op bij het bestaande product
    else {
       $_SESSION['cart'][$prod]['quantity'] += $thisProduct['quantity'];
    }
}

I fixed it like this with help from Don't Panic :

$quantityobject = $_POST['quantityobject'];

$arrayquantity = json_decode($quantityobject);

foreach ($arrayquantity as $object) {
    // Check if product exists in array
    if (isset($_SESSION['cart'][$object->product])) {
        // if so replace quantity with posted one
       $_SESSION['cart'][$object->product]['quantity'] = $object->quantity;
    }
}
  • 写回答

1条回答 默认 最新

  • doukeng7426 2018-03-15 15:31
    关注

    I think it looks a bit simpler than you're making it. You don't have to convert to objects to arrays and merge them. You can use them as-is, just iterate the array of objects and update the session data for the corresponding product.

    foreach ($posted_data as $object) {
        $_SESSION['cart'][$object->product]['quantity'] = $object->quantity;
    }
    

    You may want to include some code that checks that the product exists in the session array before trying to update it and handle that somehow, although this will create an incomplete product with only a quantity.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害