Reputation: 363
I have created a session array as follows
$_SESSION['memberInfo']['memberFirstName'] = ($first_name);
$_SESSION['memberInfo']['memberLastName'] = ($surname);
$_SESSION['memberInfo']['hash'] = ($hash);
$_SESSION['memberInfo']['templateSrc'] = ($newTemplateSrc);
in other pages where I'm trying to get the values from the array I have tried foreach and while loops without success, I can see the array in a var_dump
var_dump($_SESSION['memberInfo']);
which shows in console as
array(4) {
["memberFirstName"]=>
string(8) "Geoffrey"
["memberLastName"]=>
string(6) "Turner"
["hash"]=>
string(60) "$2y$10$YBE1tc.BK7yq6bBr/JAlWuN0H8xGdoNSAWzU4/zfd1r3v7jprNBD2"
["templateSrc"]=>
string(61) "../userDirectory/558386500/html/Geoffrey_Turner_558386500.php"
}
in the pages where im trying to itterate the array I have tried using
foreach ($_SESSION['memberInfo'] as $name)
{
$first_name = $name['memberFirstName'];
}
the responce I get shows as
Warning: Illegal string offset 'memberFirstName'
which I believe suggests the itteration is reading an empty array
I can echo out the array using
foreach ($_SESSION['memberInfo'] as $key => $val) {
echo "$key = $val\n";
}
which results in
memberFirstName = Geoffrey
memberLastName = Turner
hash = $2y$10$YBE1tc.BK7yq6bBr/JAlWuN0H8xGdoNSAWzU4/zfd1r3v7jprNBD2
templateSrc = ../userDirectory/558386500/html/Geoffrey_Turner_558386500.php
but for the life of me I cannot seem to figure out how to get each of the array values individually and assign them to a variable
Upvotes: 1
Views: 67
Reputation: 75629
Your foreach makes no sense as you looping an trying to assing single variable too many times for no benefits. So this to be removed:
foreach ($_SESSION['memberInfo'] as $name =) {
$first_name = $name['memberFirstName'];
}
as all you need is:
$first_name = $_SESSION['memberInfo']['memberFirstName'];
Upvotes: 2
Reputation: 33618
how to get each of the array values individually and assign them to a variable
You can use extract
which extracts each value and assigns it to a variable.
extract($_SESSION['memberInfo'])
this should create the following variables
$memberFirstName
, $memberLastName
, $hash
, $templateSrc
Here is a demo http://ideone.com/XYpC7n
Upvotes: 2