Jump to content

[SOLVED] Arrays in a session variable


otuatail

Recommended Posts

Thanks to those who replied to my array problem post.

What I am attempting to do is create an expanding array, and call a function that pulls a record from a database table. Then search the aray for a value.

The problem is the array will have to be in a session variable. I have tried the following without success.

 

*** php page ***
include ('functions.inc');
$me[] = array("1-1","2-2","3-3");
$_SESSION['Visits'] = $me;
visited(1);

*** functions file ***
myfunction()
{
$list = $_SESSION['Visits'];
$key = array_search($seek , $list);
echo $key . "<br>"; // displays nothing
echo count($list);  // Displays 1 but array is 3
}

 

Any sugestions on how to manage the array in a session. Help!

 

 

Link to comment
https://forums.phpfreaks.com/topic/77714-solved-arrays-in-a-session-variable/
Share on other sites

Be careful how much memory your array takes up if it is expanding.  It is almost always best to recall mysql records than store them into a php array which may exponentially wastes server memory.

 

There is an easier way to add array data into the session...

 


$_SESSION['users'][1]['id'] = 12043;
$_SESSION['users'][1]['first_name'] = 'John';
$_SESSION['users'][1]['last_name'] = 'Smith';

$_SESSION['users'][2]['id'] = 10942;
$_SESSION['users'][2]['first_name'] = 'Jane';
$_SESSION['users'][2]['last_name'] = 'Doe';

 

This format works fine with the print_r() function for debugging...here is the print_r() output for the $_SESSION structure above...

 


Array
(
   [users] => Array
       (
           [1] => Array
               (
                   [id] => 12043
                   [first_name] => John
                   [last_name] => Smith
               )

           [2] => Array
               (
                   [id] => 10942
                   [first_name] => Jane
                   [last_name] => Doe
               )

       )

)


 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.