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?