Laravel - Session and Array

3

I need to 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 retrieve the values according to each key of the array. How do I do that? I could not understand very well seeing the laravel documentation.

    
asked by anonymous 13.05.2016 / 17:15

2 answers

3

If you are using Laravel 4

The session is created as follows:

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

to display data Session::get('key.um')

For laravel 5

to create

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

to display

session()->get('key');
    
13.05.2016 / 18:22
1

If you want to add these array to a session value where you already entered the given agum, the best way would be to use the 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 array directly to specific key :

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

You can also access define values by dot notation:

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

Alternatively you can also set a array directly:

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

To get the values, you can do the following:

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

 session('session_array.name'); 

 session('session_array.1');
    
13.05.2016 / 18:33