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.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog