Jump to content
makamo66

Looping over a multidimensional array

Recommended Posts

<?php
$_SESSION["cart_item"] = array(
'cart_item' => array(
'id' => $id,
'product_name' => $product_name
));
}
$cart_items = $_SESSION["cart_item"];

foreach ($cart_items as $cart_item) {
echo $cart_item["id"] . $cart_item["product_name"];
}
?>

I have tried several variations of the foreach loop like the one above and I mostly get the error message: Notice: Array to string conversion. When I use:
var_dump($cart_items);

I get the following output:

array(1) { ["cart_item"]=> array(2) { ["id"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "3" } ["product_name"]=> array(2) { [0]=> string(19) "Adult Female Bike" [1]=> string(18) "Kids Unisex Bike" } } }

Share this post


Link to post
Share on other sites

The item in $_SESSION is called "cart_item". One item. That's what it is, right?

You then assign it to a variable named "cart_items". Plural. That does not magically make it have multiple items.

You only have the one item. You can't foreach over an item. What you need is an array of items (see the plural?) which you can then foreach over.

Share this post


Link to post
Share on other sites

The inner arrays of the multidimensional $_SESSION arrays aren't single variables, they're $_SESSION arrays defined like below.

for($i=1; $i<=6; $i++){
    
if (isset($_REQUEST["element_id_$i"])  && ($_REQUEST["element_id_$i"] != NULL) ) {
$_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"];
$id = $_SESSION["element_id_$i"];    
array_push($_SESSION["element_id"],$id);
}
$id = $_SESSION["element_id"];
    
if (isset($_REQUEST["product_$i"])  && ($_REQUEST["product_$i"] != NULL) ) {
$_SESSION["product_name_$i"] = $_REQUEST["product_$i"];
$product_name = $_SESSION["product_name_$i"];    
array_push($_SESSION["product_name"],$product_name);
}
$product_name = $_SESSION["product_name"];
}

 

Share this post


Link to post
Share on other sites

I'm not sure I understand. You posted some code earlier and asked why it didn't work, and are now saying that this other code is somehow responsible for it?

Look at your code again, but this time with proper indentation:

$_SESSION["cart_item"] = array( // <- this will be $cart_items
	'cart_item' => array( // <- so this is what the foreach will find
		'id' => $id,
		'product_name' => $product_name
	)
);

 

Share this post


Link to post
Share on other sites

I retract my original question because I don't need to make my multidimensional-array any larger after all. Please ignore my post.
 

Share this post


Link to post
Share on other sites

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.