2

Is there any way to pass an array between two pages intact?

I am building a huge array, and the building of it uses masses of memmory. I would like to be able to store the array intact and then reaccess it from another page?

If I use $x = print_r($array,true); and write it to a file, how could I then rebuild it into an array, or is there a better way altogether.

1
  • How big your array is? In numbers. And are you sure you need whole array on the next page? Commented Oct 13, 2010 at 22:49

4 Answers 4

4

You could easily store that data in the session. Like this

$_SESSION['serialized_data'] = urlencode(serialize($your_data));

and then afterwards on your second page:

$your_data = unserialize(urldecode($_SESSION[$serialized_data]));

I use this approach quite often.

Sign up to request clarification or add additional context in comments.

3 Comments

If find it more suitable writing the data to a file you can use a similar approach of serializing/unserializing.
Note that if you are using object references they cannot be serialized.
I'm not sure if this helps anyone else, but it works for my situation: I am passing the data to another page using POST. I'm also using ajax but obviously you don't have to. Using $dataToPost = urlencode(serialize($your_data)); works great for me.
2

You can store it in session ( not sure how big it is ) .. if you want to write to file .. you can do something like this:

$fp = fopen("file.php" , "w");
fwrite($fp , "<? \$array = ".var_export($array,true).";");
fclose($fp);

and then just include that file like a normal file on next page loads.

1 Comment

The serialize function is better in such situation :)
0

Passing huge amounts of data between pages generally isn't a great decision, but there can be exceptions - what are you trying to accomplish here?

I wouldn't suggest using session variables. In many cases, if the data seems to large to pass between pages, it is. In those cases, it may be useful to use a database for the information and access the database from each page.

Comments

0

Easiest way would be to use a session variable.

$_SESSION['big_array']=$big_array;

This wouldn't be particularly advisable if it's a high volume site (as the arrays will sit in memory until sessions expire) but should be fine otherwise.

You'll want to make sure you've started the session prior which, if necessary, can be done using:

session_start();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.