dongxindu8753 2018-05-17 10:01
浏览 43
已采纳

Laravel 4.2:在会话中保留多维数组数据

In my laravel 4.2 application, i want to store the cart item details in session.

cartId =>[
            0 => [
                productId => x,
                quantity  => y
            ],
            1 => [
                productId => u,
                quantity  => v
            ],
            2 => [
                productId => l,
                quantity  => m
            ]

         ]

I didn't found a way except this

Session::push('user.teams', 'developers');

for storing as array in session. but the same is not applicable here

  • 写回答

2条回答 默认 最新

  • doujiebo9849 2018-05-18 13:03
    关注

    Looks like you can insert the array directly in the session.

    $someArray = ['name' => 'John Doe', 'username' => 'jdoe'];
    
    Session::put('user', $someArray);
    

    When you want to retrieve its value just need to:

    $user = Session::get('user');
    echo $user['name'] // output: John Doe
    

    Same applicable for multidimensional array...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?