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
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
               )

       )

)


 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.