title: How can I add new items to my session array if value is not already in it? PHP
body: <p>I am trying to add new items to my array if they don't already exist but below code shows me an error:</p>
  
  <pre><code>// Check if session exists
  if(!isset($_SESSION['coupon'])){
    // Create array from session
    $_SESSION['coupon']['couponcode'] = array();
  }
  
  if(!isset($_SESSION['coupon']['couponcode'][$coupon])){
    // Add couponcode to session if it does not already exist
    $_SESSION['coupon']['couponcode'][] = $coupon;
  }
  </code></pre>
  
  <p><code>$_SESSION['coupon']['couponcode'][] = $coupon;</code></p>
  
  <p>Gives: <code>PHP Fatal error:  Uncaught Error: [] operator not supported for strings</code></p>
  
  <p>But I thought this was the way to add to the array, if I remove the brackets it just replaces the value everytime.</p>
  
  <p>I have <code>session_start();</code> everywhere at the top of my pages.</p>