" Laravel Session Array " 在 Laravel 中打印创建的会话数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27009697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:27:14  来源:igfitidea点击:

" Laravel Session Array " Printing a created session array in Laravel

phpsessionlaravellaravel-4

提问by Ronser

I have created a session array in laravelusing the code:

我使用以下代码在 laravel 中创建了一个会话数组

Session::put("backUrl", array($workout_id =>URL::previous()))   ;
  //or
Session::push("backUrl.$workout_id", URL::previous())   ;

Both works and it has been created successfully and I could see it in the debugger

两者都有效并且已成功创建,我可以在调试器中看到它

'backUrl' => array(1) [
    '78' => string (36) "http://192.241.4.104/admin/view?cs=1"
]

now I am unable to print it, The code i have used is

现在我无法打印它,我使用的代码是

echo Session::get("backUrl"[$workout_id]);

it shows a syntax error, unexpected '['error

它显示一个syntax error, unexpected '['错误

And I have also used

我也用过

echo Session::get("backUrl[$workout_id]"); 

nothing works

没有任何效果

回答by Steini

Because you keyed your entire array under the session variable "backurl".

因为您在会话变量“backurl”下键入了整个数组。

if you var_dump:

如果你 var_dump:

var_dump(Session::get("backUrl")):

I am pretty sure you get:

我很确定你会得到:

array(
    [2] => "http://previous-url"
)

So wether you go like this:

所以你可以这样:

$lastUrl = Session::get("backUrl");
echo array_keys($lastUrl)[0]; //workout-ID
echo array_values($lastUrl)[0]; //Value

Or you save your two variables seperately:

或者你分别保存你的两个变量:

Session::put("backUrl", URL::previous());
Session::put("lastWorkoutId", $workout_id);

And then read them individually:

然后单独阅读它们:

Session::get("backUrl");
Session::get("lastWorkoutId");

回答by Ronser

After several trials I have got what I wanted a session array for back button URLand thanks to @Steini for his valuable suggestions. I am posting this as it could be useful to someone...

经过几次试验,我得到了我想要的后退按钮 URL 的会话数组,感谢@Steini 的宝贵建议。我发布这个是因为它可能对某人有用......

At first I have changed using

起初我改变了使用

Session::put("backUrl", array($workout_id =>URL::previous()))   ;

to

Session::put("backUrl.$workout_id", URL::previous())    ;

Saw the Session::push tag in Laravel Docsand tried luckly it worked. Because the first one deletes the existing session array and creates a new one.

在 Laravel Docs 中看到了Session::push 标签并幸运地尝试了它的工作。因为第一个删除了现有的会话数组并创建了一个新的。

And printing the Laravel session array is as simple as printing a session with added suffix

打印 Laravel 会话数组就像打印添加后缀的会话一样简单

Session::get("sessionArrayName")['id']
   (i.e)
Session::get("backUrl")[$workout_id];

Thus got my session array printed and used it for my back button....

因此打印了我的会话数组并将其用于我的后退按钮......