phpsessionkohana

Session::instance() and push array data to session array


I have Session::instance()->get('orders') which is an array of some arrays:

$first = array('id' = 1, 'name' => 'first', 'price' => 100);
$second = array('id' => 2, 'name' => 'second', 'price' => 200);
$_SESSION['orders'] = array($first, $second);

but if I use this

Session::instance()->set('orders', array(array('id' => 3, 'name' => 'third', 'price' => 300)));

This will erase the first orders (id 1, id 2).

How can I add but not erase data arrays to session array named 'orders'? array_push() or something else?


Solution

  • Edit, didn't see your comment, it's perfect.

    Self explanatory.

    $session = Session::instance();
    
    // Find existing data
    $data = $session->get('orders');
    
    // Add new array
    $data[] = array('id' => 3, 'name' => 'new data', 'price' => 300);
    
    // Resave it
    $session->set('orders', $data);