Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 合并对象和数组_Php_Json_Object - Fatal编程技术网

Php 合并对象和数组

Php 合并对象和数组,php,json,object,Php,Json,Object,我将一些json格式的数据发布到php脚本中,这些数据需要替换会话(数组)中的现有数据 以下是我在PHP脚本中发布的内容: Array ( [0] => stdClass Object ( [product] => Bad 1 [quantity] => 2 ) [1] => stdClass Object ( [product] =&

我将一些json格式的数据发布到php脚本中,这些数据需要替换会话(数组)中的现有数据

以下是我在PHP脚本中发布的内容:

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

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

)
这是我的会话数组(
$\u session['cart']
):

我希望过帐的数量值替换会话数组中的数量值,但仅用于正确的产品。因此,如果我将数量“10”与产品“bad 1”一起发布,则只需将键“bad 1”的数量替换为该值

在发布多个产品(如我的示例中的posted对象)时也会出现这种情况

如何将对象更改为数组并将其与会话数组合并(仅替换数量)

我尝试过类似的方法,但这只适用于一个发布值

//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'];
    }
}
在“不要惊慌”的帮助下,我这样修复了它:

$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;
    }
}

我觉得它看起来比你做的要简单一点。您不必将对象转换为数组并合并它们。您可以按原样使用它们,只需迭代对象数组并更新相应产品的会话数据

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

在尝试更新产品之前,您可能希望包含一些代码来检查产品是否存在于会话数组中,并以某种方式进行处理,尽管这将创建一个数量有限的不完整产品。

首先,您必须编写一些代码。你试过写代码吗?这看起来并不复杂,特别是因为会话已经使用产品作为其密钥。可能你把它误认为是免费使用编码服务。我们不是,但如果你在附近逗留足够长的时间,一定会有一只代表猎犬soon@RiggsFolly我得到了类似的结果,添加到数组中,但这只发布了一个值,并检查数组中是否存在该值。我将把它添加到我的问题中。但是我一直在研究如何使用另一个数组/对象来实现这一点。然后你需要写一个循环谢谢,我也通过检查解决了这个问题。我在我的问题中发布了解决方案。不客气。我建议添加一些真正能够处理缺失产品的东西,而不是仅仅忽略它们。我假设提出请求的任何人/任何人都希望更新其发送的所有产品的数量,因此无法为他们中的任何人这样做似乎可能是他们应该知道的错误情况。
foreach ($posted_data as $object) {
    $_SESSION['cart'][$object->product]['quantity'] = $object->quantity;
}