[英]Extract specific data from multidimensional array
I have a small problem.我有一个小问题。 I'm trying to extract data from [user]
but I can seem to get it right.我正在尝试从[user]
中提取数据,但我似乎做对了。 Can someone please give me one example of how to extract for example the users id and from there I'm golden.有人可以给我一个示例,说明如何提取例如用户 id 并从那里我是金子。 This is the $_SESSION
array (what the session contains), if that's any help.这是$_SESSION
数组(session 包含的内容),如果有帮助的话。
Array ( [__default] => Array (
[session.counter] => 3
[session.timer.start] => 1307209662
[session.timer.last] => 1307209693
[session.timer.now] => 1307209701
[session.client.browser] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
[registry] => JRegistry Object (
[_defaultNameSpace] => session
[_registry] => Array (
[session] => Array (
[data] => stdClass Object ( )
)
)
[_errors] => Array ( )
)
[user] => JUser Object (
[id] => 0
[name] =>
[username] =>
[email] =>
[password] =>
[password_clear] =>
[usertype] =>
[block] =>
[sendEmail] => 0
[gid] => 0
[registerDate] =>
[lastvisitDate] =>
[activation] =>
[params] =>
[aid] => 0
[guest] => 1
[_params] => JParameter Object ( [_raw] =>
[_xml] =>
[_elements] => Array ( ) [_elementPath] => Array ( [0] => C:\xampp\htdocs\libraries\joomla\html\parameter\element )
[_defaultNameSpace] => _default
[_registry] => Array (
[_default] => Array (
[data] => stdClass Object ( )
)
)
[_errors] => Array ( )
)
[_errorMsg] => [_errors] => Array ( )
)
[session.token] => 971893bd69fff85ea2a006788a28b15d
)
[referrerid] => AUPRS-JOHNDOE
)
$user = $_SESSION['user'];
echo $user->id;
This seems to be what you need.这似乎是你需要的。
This would output the user name: (if there was any)这将 output 用户名:(如果有的话)
print $_SESSION["__default"]["user"]->name;
You first need to overcome the __default
key index within the $_SESSION array.您首先需要克服 $_SESSION 数组中的__default
键索引。 Likewise is user
an array key.同样是user
一个数组键。 But what the user
entry contains is an object, so you need the ->
object arrow thingy to finally access the name
.但是user
条目包含的是 object,因此您需要->
object 箭头才能最终访问name
。
Also I would not rely on __default
being a given.此外,我不会依赖__default
是给定的。 Don't know anything about Joomla, but the $_SESSION array structure might be more flexible.对Joomla一无所知,但$_SESSION数组结构可能更灵活。 Hencewhy it's advisable to find a function which instead fetches the user object for you.因此,建议找到一个 function 来代替为您获取用户 object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.