DCM Posted September 26, 2010 Share Posted September 26, 2010 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 https://forums.phpfreaks.com/topic/214459-multi-dimension-_session-arrays/ Share on other sites More sharing options...
rwwd Posted September 26, 2010 Share Posted September 26, 2010 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 https://forums.phpfreaks.com/topic/214459-multi-dimension-_session-arrays/#findComment-1115989 Share on other sites More sharing options...
DCM Posted September 26, 2010 Author Share Posted September 26, 2010 Thanks so would i reference the data as: $_SESSION['hosts'][$x][0]; or $_SESSION['hosts[$x][0]']; Link to comment https://forums.phpfreaks.com/topic/214459-multi-dimension-_session-arrays/#findComment-1115995 Share on other sites More sharing options...
rwwd Posted September 26, 2010 Share Posted September 26, 2010 The former rather than the latter. Well that's the way I would try it! The latter version isn't a multidimensional array anyway, that's just dynamically altering the key name of the array: ergo: 1 dimensional. Rw Link to comment https://forums.phpfreaks.com/topic/214459-multi-dimension-_session-arrays/#findComment-1116002 Share on other sites More sharing options...
DCM Posted September 27, 2010 Author Share Posted September 27, 2010 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 https://forums.phpfreaks.com/topic/214459-multi-dimension-_session-arrays/#findComment-1116200 Share on other sites More sharing options...
trq Posted September 27, 2010 Share Posted September 27, 2010 Variables are not interpolated within single quotes. Your also not creating a multi-dimensional array if that is indeed how you are accessing it. Link to comment https://forums.phpfreaks.com/topic/214459-multi-dimension-_session-arrays/#findComment-1116202 Share on other sites More sharing options...
DCM Posted September 27, 2010 Author Share Posted September 27, 2010 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 https://forums.phpfreaks.com/topic/214459-multi-dimension-_session-arrays/#findComment-1116208 Share on other sites More sharing options...
trq Posted September 27, 2010 Share Posted September 27, 2010 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 https://forums.phpfreaks.com/topic/214459-multi-dimension-_session-arrays/#findComment-1116242 Share on other sites More sharing options...
rwwd Posted September 27, 2010 Share Posted September 27, 2010 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 https://forums.phpfreaks.com/topic/214459-multi-dimension-_session-arrays/#findComment-1116244 Share on other sites More sharing options...
DCM Posted September 27, 2010 Author Share Posted September 27, 2010 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 https://forums.phpfreaks.com/topic/214459-multi-dimension-_session-arrays/#findComment-1116487 Share on other sites More sharing options...
jcbones Posted September 27, 2010 Share Posted September 27, 2010 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 https://forums.phpfreaks.com/topic/214459-multi-dimension-_session-arrays/#findComment-1116497 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.