How to 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?

I’m not clear what is you want in the end?

Are you saying you want an multi-dimensional associative array?

//example array set-up
$exampleArray = [['id' => 1, 'value' => 'someValue1'],['id' => 4, 'value' => 'someValue2'],['id' => 18, 'value' => 'someValue3']];
var_dump($exampleArray);

//or access data directly (or in a loop)
echo 'ID CHECK: ' . $exampleArray[2]['id'] . '<br>';

for($i=0; $i<count($exampleArray); $i++){
	echo 'DATA CHECK: ' . $exampleArray[$i]['id'] . ' / ' . $exampleArray[$i]['value'] . '<br>';
}

Where does:

$cart_items[“id”] = $id;
$cart_items[“quantity”] = $quantity;

Come from?

What data is it populated with? (seems to be array perhaps?)

like anything else

$cart_items = [$id => $quantity];
Sponsor our Newsletter | Privacy Policy | Terms of Service