Session and array handling

This is going to sound strange, but I couldn’t find what I’m looking for using google…lol

I’m pretty sure its something ease but I can’t seem to put the code together.
I’m pulling in data from a database to $results =>: userID, firstName, lastName etc. What I’m trying to do is take the query results and push it into the session array

session_start();
$_SESSION[‘loggedInUser’] = array();

End of the day I like to take the results from the database query($results) and push it into the session array:
$_SESSION[‘loggedInUser’] = $results[‘userID’] => ‘232’;
$_SESSION[‘loggedInUser’] = $results[‘firstName’] => ‘Bob’;
$_SESSION[‘loggedInUser’] = $[ ‘lastName’] => ‘Smith’

and the option to access the results
echo $_SESSION[‘loggedInUser’] [‘userID’];

How do i do this?

;

You have an array $results from database ($results[‘userID’] = …, $results[‘firstName’] = …, etc…) so do that : $_SESSION[‘loggedInUser’] = $results;

Try this:

  session_start();

 // delete all sessions
 $_SESSION = [];

// delete this session
  $_SESSION['loggedInUser'] = []; 

  $_SESSION['loggedInUser']['userId']     = '123456';
  $_SESSION['loggedInUser']['firstName']  = 'firsName';
  $_SESSION['loggedInUser']['lastName']   = 'lastName';

// add sub array sessions
  $_SESSION['loggedInUser']['sub']['001'] = $_SESSION['loggedInUser'];
  $_SESSION['loggedInUser']['sub']['002'] = $_SESSION['loggedInUser'];
  $_SESSION['loggedInUser']['sub']['003'] = $_SESSION['loggedInUser'];


// print results
echo '<pre>'; print_r($_SESSION); echo '</pre>';

// remove session variable
unset($_SESSION['loggedInUser']['sub']['002'] );

// print new results and add linefeeds using &lt;pre&gt;
echo '<hr><pre>'; print_r($_SESSION); echo '</pre>';

Output

Array
(
    [loggedInUser] => Array
        (
            [userId] => 123456
            [firstName] => firsName
            [lastName] => lastName
            [sub] => Array
                (
                    [001] => Array
                        (
                            [userId] => 123456
                            [firstName] => firsName
                            [lastName] => lastName
                        )

                    [002] => Array
                        (
                            [userId] => 123456
                            [firstName] => firsName
                            [lastName] => lastName
                            [sub] => Array
                                (
                                    [001] => Array
                                        (
                                            [userId] => 123456
                                            [firstName] => firsName
                                            [lastName] => lastName
                                        )

                                )

                        )

                    [003] => Array
                        (
                            [userId] => 123456
                            [firstName] => firsName
                            [lastName] => lastName
                            [sub] => Array
                                (
                                    [001] => Array
                                        (
                                            [userId] => 123456
                                            [firstName] => firsName
                                            [lastName] => lastName
                                        )

                                    [002] => Array
                                        (
                                            [userId] => 123456
                                            [firstName] => firsName
                                            [lastName] => lastName
                                            [sub] => Array
                                                (
                                                    [001] => Array
                                                        (
                                                            [userId] => 123456
                                                            [firstName] => firsName
                                                            [lastName] => lastName
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

Array
(
    [loggedInUser] => Array
        (
            [userId] => 123456
            [firstName] => firsName
            [lastName] => lastName
            [sub] => Array
                (
                    [001] => Array
                        (
                            [userId] => 123456
                            [firstName] => firsName
                            [lastName] => lastName
                        )

                    [003] => Array
                        (
                            [userId] => 123456
                            [firstName] => firsName
                            [lastName] => lastName
                            [sub] => Array
                                (
                                    [001] => Array
                                        (
                                            [userId] => 123456
                                            [firstName] => firsName
                                            [lastName] => lastName
                                        )

                                    [002] => Array
                                        (
                                            [userId] => 123456
                                            [firstName] => firsName
                                            [lastName] => lastName
                                            [sub] => Array
                                                (
                                                    [001] => Array
                                                        (
                                                            [userId] => 123456
                                                            [firstName] => firsName
                                                            [lastName] => lastName
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.