My foreach returns Warning: Illegal string offset 'productname'
<p>This is my first ever time posting something on stackoverflow. I'm a relatively new programmer in the likes of Php and I have this assignment where I have to make a shopping cart</p>

<p>I apologise if this is messy or can't be made out. I have been searching on these forums for about 2 days now and I cant really find myself a solutio</p>

<p>Now my problem is as follows </p>

<p>I get my product data from the database -> then I click on a product -> it goes to a page where it adds the productid, productname and price to a session with array_push and then it ends up back on the same page with where the cart is (the cart is on the same page as the productline)</p>

<p>Now I received the session just fine and with var dumps I can see that the array is completely filled even in the foreach. </p>

<p>All I want to do is show the productid/productname/price seperatly.</p>

<blockquote>
  <blockquote>
    <p>array(6) { ["productid"]=> string(1) "6" ["productname"]=> string(11) "solexfiets6" ["price"]=> string(5) "12.99" [0]=> string(1) "5" [1]=> string(11) "solexfiets5" [2]=> string(5) "16.99" }</p>
  </blockquote>
</blockquote>

<p>Up ahead is the code I am using. Maybe someone here can explain me what is going on</p>

<pre><code>session_start();

if(isset($_SESSION["cart"]) &amp;&amp; count($_SESSION["cart"])&gt; 0)
{

echo '&lt;div id="cd-cart"&gt;';
echo '&lt;h2&gt;Cart&lt;/h2&gt;';
echo '&lt;ul class="cd-cart-items"&gt;';
echo '&lt;li&gt;';


foreach($_SESSION['cart'] as $id =&gt; $value) {
    //var_dump($_SESSION["cart"]); // this var_dump
    //echo $id; //
    $products = $id;
    echo $value["productname"] . '&lt;br/&gt;';
}
echo '&lt;/li&gt;';
echo '&lt;/ul&gt;';
echo '&lt;div class="cd-cart-total"&gt;';
echo '&lt;p&gt;Total &lt;span&gt;$39.96&lt;/span&gt;&lt;/p&gt;';
echo '&lt;/div&gt;';

echo '&lt;a href="#0" class="checkout-btn"&gt;Checkout&lt;/a&gt;';
echo '&lt;p class="cd-go-to-cart"&gt;&lt;a href="#0"&gt;Go to cart page&lt;/a&gt;&lt;/p&gt;';
echo '&lt;/div&gt;';

}
</code></pre>

<p>Thank you kindly for any help</p>

<pre><code>     foreach($_SESSION['cart'] as $id =&gt; $value) {
     print_r($value); //outputs 6solexfiets612.995solexfiets516.99 
     (it puts everything in the session in a string)

    //var_dump($_SESSION["cart"]);
    //echo $id; //
    //$products = $id;
    //echo $value["productname"] . '&lt;br/&gt;';
}
</code></pre>

<p>The way I send my products to this page is by sending the productid, productname and price in the url when someone clicks on order to add_to_cart.php which then does this</p>

<pre><code>&lt;?php
session_start();

if(empty($_SESSION['cart'])){
$_SESSION['cart'] = array();
}

array_push($_SESSION['cart'], $_GET['productid'], $_GET['productname'], $_GET['price']);



?&gt;

 &lt;p&gt;Het product is toegevoegd aan uw winkelwagen&lt;/p&gt; &lt;a href="../cart.php"&gt;Naar winkelwagen&lt;/a&gt;
</code></pre>

<p>By all the help I have received I found out that I'm sending the data the wrong way. Is there a better way to send the products to the cart and then looping through it?</p>

<p>I have made a followup question on this question right here with all the information on my code</p>

<p><a href="https://stackoverflow.com/questions/35677015/sessions-issue-with-my-shopping-cart">Sessions/foreach issue with my shopping cart</a></p>

<p>I'm sorry for the inconvenience</p>