U declared 2 times $_SESSION['cart']
. Remove second declare and you should be good to go.
EDIT :
U don't use correct syntax for array_push.
PHP.net : array_push()
If i understood correctly - the index of key (numeric since array_push) bothers you.
U can bypass by using this code :
$_SESSION['cart'] = array();
$_SESSION['cart']['itemName'] = 12.50;
$_SESSION['cart'] = $_SESSION['cart'] + array("blabla" => 13.99);
print "<pre>";
print_r($_SESSION);
print "</pre>";
Result :
Array
(
[cart] => Array
(
[itemName] => 12.5
[blabla] => 13.99
)
)
LAST EDIT : (!?!)
function set_item_price($itemName, $itemPrice)
{
if(!isset($_SESSION['cart'][$itemName]['itemPrice']))
$_SESSION['cart'][$itemName]['itemPrice'] = $itemPrice;
else
echo "Item Already exists \n <br>";
}
function add_to_cart($itemName, $quantity)
{
if(!isset($_SESSION['cart'][$itemName]['quantity']))
$_SESSION['cart'][$itemName]['quantity'] = $quantity;
else
$_SESSION['cart'][$itemName]['quantity'] += $quantity;
}
// Adding item1 - price 12.50
set_item_price("item1", 12.50);
// Adding item1 - quantity - 2 & 3
// OutPut will be 5
add_to_cart("item1", 2);
add_to_cart("item1", 3);
// Adding item3 - price 15.70
set_item_price("item3", 15.70);
// Adding item1 - quantity - 5
add_to_cart("item3", 5);
print "<pre>";
print_r($_SESSION);
print "</pre>";
?>
RESULT :
Array
(
[cart] => Array
(
[item1] => Array
(
[itemPrice] => 12.5
[quantity] => 5
)
[item3] => Array
(
[itemPrice] => 15.7
[quantity] => 5
)
)
)
Otherwise check on this thread :
Stackoverflow : how-to-push-both-value-and-key-into-array-php