Q:

increase qty by 1 if item already exists in cart

I want to use a foreach loop to loop through all products in the cart and if it exists and the user adds it to the cart again, it should increase that item's total by 1 but I am not sure how to implement this. What I want to happen is that if a user clicks "add to cart" on an item it gets added to the cart with quantity of 1. If they click "add to cart" again for the same item, it should change to quantity of 2 for that item. 
    $product_id = $_POST['id'];

    if (!isset($_SESSION['cart_array'])) {
        $_SESSION['cart_array'] = array(
            "item_id" => $product_id,
            "quantity" => 1
        );
    }
    else {
        if (in_array($product_id, $_SESSION['cart_array'])) {
            // add 1 to items already in cart
            $_SESSION['cart_array'] = $product_id . " " . "Item exists, add 1";
        }
        else {
            array_push($_SESSION['cart_array'], array(
                "item_id" => $product_id,
                "quantity" => 1
            ));
        }
    }

I have updated this question: 
I have managed to get it to increase for only the first item in the array but need it to do the same for each item in the cart:
if(!isset($_SESSION['cart_array'])) {
    $_SESSION['cart_array'] = array("item_id" => $product_id, "quantity" => 1);

        } else {

            if(in_array($product_id, $_SESSION['cart_array'])) {
                $_SESSION['cart_array']= array("item_id" => $product_id, "quantity" => $_SESSION['cart_array']['quantity'] + 1);

            } else {

        array_push($_SESSION['cart_array'], array("item_id" => $product_id, "quantity" => 1));

            }
    }

A:

Why not use an associative array?
<?php

$productID = $_POST['id'];

session_start();

if (!isset($_SESSION['cart_array']))
{
    $_SESSION['cart_array'] = [];
}
if (!isset($_SESSION['cart_array'][$productID]))
{
    $_SESSION['cart_array'][$productID] = [
        "quantity" => 0
    ];
}

$_SESSION['cart_array'][$productID]['quantity']++;

