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. Can someone please give me one example of how to extract for example the users id and from there I'm golden. This is the $_SESSION
array (what the session contains), if that's any help.
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)
print $_SESSION["__default"]["user"]->name;
You first need to overcome the __default
key index within the $_SESSION array. Likewise is user
an array key. But what the user
entry contains is an object, so you need the ->
object arrow thingy to finally access the name
.
Also I would not rely on __default
being a given. Don't know anything about Joomla, but the $_SESSION array structure might be more flexible. Hencewhy it's advisable to find a function which instead fetches the user object for you.
精彩评论