Jump to content

How to make an associative array with $key => $value pairs


makamo66

Recommended Posts

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?

Link to comment
Share on other sites

You can use variables as the index when you create an associative array.

Considering that, and as you already have the variables that contain the id and quantity, the solution should be pretty obvious...

Edited by maxxd
typo
Link to comment
Share on other sites

Here's an example

if ($_SERVER['REQUEST_METHOD']=='POST') {
    $post = array_map('trim', $_POST);
    if (!empty($post['productid']) && !empty($post['qty'])) {
        if (!isset($_SESSION['cart'][$_POST['productid']])) {
            $_SESSION['cart'][$_POST['productid']] = 0;
        }
        $_SESSION['cart'][$_POST['productid']] += $_POST['qty'];
    }
}

from an earlier topic of yours !

Link to comment
Share on other sites

22 hours ago, maxxd said:

You can use variables as the index when you create an associative array.

Considering that, and as you already have the variables that contain the id and quantity, the solution should be pretty obvious...

When I use this:

$cart_items[$id] = $quantity;

or this:

$cart_items = array($id => $quantity);

I get the error: Illegal offset type

Link to comment
Share on other sites

$id is defined like this:

for($i=0; $i<=5; $i++){
if (isset($_REQUEST["element_id_$i"]) ) {
$_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"];
$id = $_SESSION["element_id_$i"];    
array_push($_SESSION["element_id"],$id);
}
$id = $_SESSION["element_id"];
}
 and $quantity is similar.

Link to comment
Share on other sites

// $id may or may not exist, doesn't matter
for ($i = 0; $i <= 5; $i++) {
	if (isset($_REQUEST["element_id_$i"])) {
		// Now you're checking _any_ input for anything called element_id_{0-5} and assigning it to the session variable.
		//	$id is still potentially undefined
		$_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"];

		// OK, so now $id is a string or integer or float or whatever (validate your input, please)
		$id = $_SESSION["element_id_$i"];

		// $_SESSION['element_id'] is an array because you're using array_push
		array_push($_SESSION["element_id"], $id);
	}

	// And now $id is an array because you're overwriting the value you've already put in there
	// Also, at this point if $_REQUEST['element_id_{0-5}'] _isn't_ set, $_SESSION['element_id'] should
	//	not exist and you'll get an error
	$id = $_SESSION["element_id"];
}

So $id is an array.

You can use variables as array keys, but the value of those variables has to be a string or integer - you can't use an array as an index in another array.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.