Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a multidimensional array that is something like this

  • [0]string
  • [1]-->[0]string,[1]string,[2]string
  • [2]string
  • [3]string
  • [4]-->[0]string,[1]string,[2]string[3]string,[4]string,[5]INFO

(I hope that makes sense)

where [1] and [4] are themselves arrays which I could access INFO like myArray[4][5].

The length of the nested arrays ([1] and [4]) can varry.

I use this method to store, calculate, and distribute data across a pretty complicated form.

Not all the data thats storred in the array makes it to an input field so its not all sent to the next page when the form's post method is called.

I would like to access the array the same way on the next page as I do on the first.


Thoughts:

Method 1:

I figure I could load all the data into hidden fields, post everything, then get those values on the second page and load themm all back into an array but that would require over a hundred hidden fields.

Method 2:

I suppose I could also use .join() to concatenate the whole array into one string, load that into one input, post it , and use .split(",") to break it back up. But If I do that im not sure how to handel the multidimensional asspect of it so that I still would be able to access INFO like myArray[4][5] on page 2.

I will be accessing the arrary with Javascript, the values that DO make it to inputs on page 1 will be accessed using php on page 2.


My question is is there a better way to acomplish what I need or how can I set up the Method 2 metioned above?



This solved my problem:

var str = JSON.stringify(fullInfoArray);
sessionStorage.fullInfoArray = str;

var newArr = JSON.parse(sessionStorage.fullInfoArray);

alert(newArr[0][2][1]);
share|improve this question
1  
JSON.stringify/JSON.parse – Ian Apr 9 '13 at 5:50
    
Can you use sessionStorage? – Ja͢ck Apr 9 '13 at 5:59
    
@Jack Yes I can, from what I see though that would still leave me accessing the values with something like sessionStorage.myValue right? Or could I then trun it all back into an array? – DelightedD0D Apr 9 '13 at 6:24
1  
@DelightedD0D You can combine sessionStorage with JSON.stringify() and JSON.parse(). – Ja͢ck Apr 9 '13 at 6:25
    
gotcha ok @Ian mentioned JSON.stringify() and JSON.parse() too, im reading that documentation now, looks like just what I need – DelightedD0D Apr 9 '13 at 6:27
up vote 2 down vote accepted

If possible, you can use sessionStorage to store the string representation of your objects using JSON.stringify():

// store value
sessionStorage.setItem('myvalue', JSON.stringify(myObject));

// retrieve value
var myObject = JSON.parse(sessionStorage.getItem('myvalue'));

Note that sessionStorage has an upper limit to how much can be stored; I believe it's about 2.5MB, so you shouldn't hit it easily.

share|improve this answer
    
+1 for sessionStorage, does IE8 support it ? – Amogh Talpallikar Apr 9 '13 at 10:35
    
@Amogh yes it should be available in IE8 onwards. – Ja͢ck Apr 9 '13 at 11:22

Keep the data in your PHP Session and whenever you submit forms update the data in session. Every page you generate can be generated using this data.

OR

If uou are using a modern browser, make use of HTML5 localStorage.

OR

You can do continue with what you are doing :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.