Laravel, getting specific array index from session

妖精的绣舞 提交于 2020-04-30 07:07:06

问题


Currently, I'm dumping an array that is held within my laravel session, which successfully dumps the array:

<?php dd(Session::get('Tokens'));?>

This dumps an array with three elements, each with its own index

array(
   "userToken":"value",
   "secondToken":"value",
   "thirdToken":"value",
);

I keep running into errors trying to get specifically the userToken. I've tried get('Tokens[userToken]') but It's expecting a string only

How should I change this to be able to access any array key specifically


回答1:


You can use array dereferencing and add a required index directly to value, returned by Session::get('Tokens'):

echo Session::get('Tokens')['userToken'];



回答2:


you can try this too

$value = session('Tokens');

and you can use $value like and array and you'll be able to call each value with the index like

$value['userToken']

hope it help



来源:https://stackoverflow.com/questions/54561150/laravel-getting-specific-array-index-from-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!