Jump to content

multi dimension $_SESSION arrays?


DCM

Recommended Posts

Hi, i am having trouble retriving data from a 2D array in my php code an was wondering if i should be using a session array instead to keep track of the contents of the array between refreshes of the page.

 

I am currently working with an array in the form of :

$hosts[][]

// $hosts[$x][0] - the hostname
// $hosts[$x][1] - the ip address
// $hosts[$x][2] - the operating system

 

However, i seem to loose the ability to read data from this array when the user creates a refresh event (ie clicks a button/hyperlink).

 

Should i be storing the data in a $_SESSION array, if so what is the correct syntax to use, i have only ever stored single values in $_SESSION variables.

 

Thanks for reading.

Link to comment
Share on other sites

Just treat the $_SESSION array as you would a normal array, just remember to use session_start() at the beginning of each page using the values, as long as they are set, you can use them where you want as they are globals.

 

Rw

Link to comment
Share on other sites

I can now access data from within my session array as below

 

echo $_SESSION['hosts[5][1]'];

 

However i do not seem to be able to access the results when refernceing the array with a variable, for example:

 


$x=5;
echo $_SESSION['hosts[$x][1]'];

 

Is this a limitation or is there a way around it?

 

Thanks again for any advice :)

 

 

 

 

 

 

Link to comment
Share on other sites

The way I understood a php array to work as in java for example is as below:

 


$hosts[x][y];

// Where x tracks how many hosts are stored in the array and y stores any number of
// details about record x, i.e.

$hosts[0][0]  // = The hostname for the first item in the array
$hosts[0][1]  // = The ip addressfor the first item in the array
$hosts[0][2]  // = The operating system for the first item in the array

$hosts[1][0]  // = The hostname for the second item in the array
$hosts[1][1]  // = The ip addressfor the second item in the array
$hosts[1][2]  // = The operating system for the second item in the array

 

What i am trying to do is hold this data as a session array so upon a page reload the data in the array is not lost, as below:

 

$_SESSION['hosts[0][0]']  // = The hostname for the first item in the array
$_SESSION['hosts[0][1]']  // = The ip addressfor the first item in the array
$_SESSION['hosts[0][2]']  // = The operating system for the first item in the array

$_SESSION['hosts[1][0]']  // = The hostname for the second item in the array
$_SESSION['hosts[1][1]']  // = The ip addressfor the second item in the array
$_SESSION['hosts[1][2]']  // = The operating system for the second item in the array

 

When referencing this array I have no problem using intergers to reference and output the data, i.e.

 

$_SESSION['hosts[0][0]'] = "hosta";
echo $_SESSION['hosts[0][0]'] ;

// outputs hosta

 

However:

 

$_SESSION['hosts[0][0]'] = "hosta";
$x=0;
echo $_SESSION['hosts[$x][0]'] ;

// returns no output

 

So the above becomes an issue when trying to output results from the array using for loops.

 

 

 

 

 

 

 

Link to comment
Share on other sites

This....

 

 

$_SESSION['hosts[0][0]'] = "hosta";

 

 

creates a single dimension array with the key of 'hosts[0][0]'

 

You woould access that via....

 

 

$x=0;echo $_SESSION["hosts[$x][0]"];

 

 

To create a multi-dimensional array (as you almost got right in your previous post) would be....

 

 

$_SESSION['hosts'][0][0] = "hosta";

 

 

Then to access it....

 

 

$x=0;echo $_SESSION['hosts'][$x][0];

 

Link to comment
Share on other sites

The latter version isn't a multidimensional array anyway, that's just dynamically altering the key name of the array: ergo: 1 dimensional.

 

 

I did tell you a couple of posts back, but thorpe has expressed it better...

 

Rw

Link to comment
Share on other sites

Guys thanks a lots that works for me now, i know it must be frustrating whe us newbies ask such basic stuff!

 

I was getting hung up on how you define arrays in the likes of java, the php syntax just seems a bit weird but I understand it now.

 

One more question if i may, am i correct in assuming this syntax is only applicable for multi-dimensional arrays in $_SESSION arrays and that a standard multi-dimension array in php could still be defined as:

 

$myarray[0][0 ];

 

Thanks again for the advice it really has helped me out :)

 

 

Link to comment
Share on other sites

I think you are over analyzing this.

 

$_SESSION is an array .PERIOD(.)

 

It is treated just like any other array.

 

$myarray['hosts'][0][0] = 'hosta';
//is equal to:
$_SESSION['hosts'][0][0] = 'hosta';

 

The only difference is, the first array is only valid in the runtime, and the second is stored on the server to be accessed later.

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.