Laravel,从会话中获取特定的数组索引 [英] Laravel, getting specific array index from session

查看:311
本文介绍了Laravel,从会话中获取特定的数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在转储保存在laravel会话中的数组,该数组成功转储了该数组:

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",
);

我一直在尝试专门获取userToken时遇到错误.我已经尝试过get('Tokens[userToken]'),但是只期望一个字符串

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

推荐答案

您可以使用数组解引用并将所需的索引直接添加到由Session::get('Tokens')返回的值:

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

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

这篇关于Laravel,从会话中获取特定的数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆