2

Have tried a number of permutations to get a value from a PHP session. The session is an array of objects I think with key value pairs. This is the structure as outputted by a key/value foreach loop

Array
(
    [laboratory_roster] => Array
        (
            [employee_entrance] => stdClass Object
                (
                    [step] => employee_entrance
                    [employee_first_name] => asdfasd
                    [employee_last_name] => fasdfasdfv
                    [employee_access_code] => valid
                    [employee_email] => [email protected]
                    [employee_state_origin] => NY
                    [employee_kit_for_whom] => employee_kit_for_employee
                )

        )

)

This is the foreach loop that I wrote to display the output above:

   foreach($_SESSION['laboratory_roster']['employee_entrance'] as $key=>$value)
    {
        // and print out the values
        echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
    }

What I wish to do is simply assign the value of the innermost value to a variable. Nothing works. Have tried:

$first_name = $_SESSION['employee_entrance']['employee_first_name'];

and this...

$first_name = $_SESSION['employee_entrance'][1];

and this...

$first_name = $_SESSION['laboratory_roster']['employee_entrance']['employee_first_name'];

and this...

 $first_name = $_SESSION['laboratory_roster']['employee_entrance']['employee_first_name'][0];

Nothing works! It's probably so simple how to get the innermost value into a PHP variable, but I am not getting it. Thanks for help!

2 Answers 2

1

Two ways:
1 Convert to stdClass: $stdClass = json_decode(json_encode($_SESSION)); Then access with [].
2 Convert to array: $array = json_decode(json_encode($_SESSION), true); Then access with ->.

Sign up to request clarification or add additional context in comments.

1 Comment

The other way around.
1

Try:

$first_name = $_SESSION['laboratory_roster']['employee_entrance']->employee_first_name;

As employee_entrance is an object (instance of stdClass) and not an array.

4 Comments

tried this as well: $first_name = $_SESSION['laboratory_roster']['employee_entrance']->employee_first_name;
@Rachel it has to work, according to the example posted. See 3v4l.org/00XMi
@Oliver it doesn't though. if I do this.. I get nothing rendered: $first_name = $_SESSION['[laboratory_roster'] ['employee_entrance']->employee_first_name; echo('First Name' . $first_name);
Something must have been cached/wonky. Working now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.