• PHP Help
  • make an associative array with $key => $value pairs

This is the cart array:
$cart = array( "1" => "2", "3" => "4", "5" => "6");
var_dump($cart);
array(3) { [1]=> string(1) "2" [3]=> string(1) "4" [5]=> string(1) "6" }
This is the cart items array:
$cart_items = array();
$cart_items["id"] = $id;
$cart_items["quantity"] = $quantity;
var_dump($cart_items);
array(2) { ["id"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["quantity"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } }

I want the key to be "id" and the value to be "quantity" like it is for the cart array. How do I define my cart_items array using variables?

The only caveat is that - given the output you've shown - $id and $quantity appear to be arrays. Not only that, they appear to be the same array. So you'll have to figure out why that is and correct it before you implement the correct solution that Weedpacket has shown you.

    Write a Reply...