Laravel-Session and Array

I need to make create an array:

array(
   0 => 0,
   1 => 0,
   2 => 1,
   3 => 0,
   4 => 0,
   5 => 1,
   6 => 0)

Within a session:

session('session_array')

And then individually add and redeem the values according to each key of the array. How do I do that? I couldn't quite understand seeing laravel's documentation.

Author: Kelvym Miranda, 2016-05-13

2 answers

If you are using Laravel 4

The session is created as follows:

Session::put('key',['um','dois']);

To display the Data Session::get('key.um')

For laravel 5

To create

session()->put('key','value');

To display

session()->get('key');
 3
Author: Miguel Batista, 2016-05-13 16:22:30

If you want to add these arrays into a session value where you had already entered a given agum, the best way would be to use the method push

session()->put('session_array', 1);

session->push('session_array', 2);

session->push('session_array', 2);

var_dump(session('session_array')); // [1, 2, 3]

You can also set the array directly on the specific key:

session()->put('session_array', $array)

You can also access set values through dot notation:

 session('session_array.nome', 'Wallace');
 session('session_array.1', 10);
 session('session_array.2', 20);

Alternatively you can also define a array directly:

session(['session_array' => [1, 2, 3, 4, 5]);

To take the values, you can do as follows:

  //equivale a $_SESSION['session_array']['name']

 session('session_array.name'); 

 session('session_array.1');
 1
Author: Wallace Maxters, 2016-05-13 16:33:37