title: How to create an array of objects and store it in a cookie dynamically
body: <p>I'am trying to create a cookie in ZF2 controller. When I am on a "hotelPage" the controller is supposed to create a cookie with the value of the hotel. I need to add dynamically data to the cookie depending on the hotelPage I am currently on. My code sofar:</p>
  
  <pre><code>                $cookieData = $this-&gt;getRequest()-&gt;getCookie();
              if(empty($cookieData-&gt;recently_viewed)){ // cookie doesn't exists                   
                  // get data from hotel object
                  $recentlyViewedHotel = array('hotelId' =&gt; $hotel-&gt;id, 'hotelName' =&gt; $hotel-&gt;name);
                  array_push($hotels, $recentlyViewedHotel);
                  System::setCookie("recently_viewed", json_encode($hotels) );
              } else{ // cookie already created, add more values to the cookie
                  $recentlyViewedHotel = array('hotelId' =&gt; $hotel-&gt;id, 'hotelName' =&gt; $hotel-&gt;name);
                  System::setCookie("recently_viewed", json_encode($hotels) );
              }
  </code></pre>
  
  <p>The output that I get is an array of one object that is always overwritten. Can anyone tell me what am I doing wrong? </p>