I have a form which asks users for their educational details, course details and technical details. When the form is submitted the page goes to a different page to run processes on the information and save parts to a database. HOWEVER(!) I then need to return the page back to the original page, where having access to the original post information is needed.
I thought it would be simple to copy (by value) the multi-dimensional (md) $_POST array to $_SESSION['post']
$_SESSION['post'] = $_POST;
However this only appears to place the top level of the array into $_SESSION['post'] not doing anything with the children/sub-arrays.
An abbreviated form of the md $_POST array is as follows:
[formid] => 2
[edu] => Array
['id'] => Array
 => new_1
 => new_2
['nameOfInstitution'] => Array
 => 1
 => 2
['qualification'] => Array
 => blah
 => blah
['grade'] => Array
 => blah
 => blah
[vID] => 61
[Submit] => Save and Continue
If I echo $_SESSION['post']['formid'] it writes "2", and if I echo $_SESSION['post']['edu'] it returns "Array". If I check that edu is an array (is_array($_SESSION['post']['edu])) it returns true. If I echo $_SESSION['post']['edu']['id'] it returns array, but when checked (is_array($_SESSION['post']['edu]['id'])) it returns false and I cannot echo out any of the elements.
How do I successfully copy (by value, not by reference) the whole array (including its children) to an new array?
THANKS EVERYONE FOR RESPONDING - UPDATE
The issue was caused my irrelevant quote marks in the naming of fields in my form.
if this is your real print_r output, then you have some unwanted chars in your array keys.
Array ( [formid] => 2 [edu] => Array //<--- $_SESSION['post']['edu'] ( ['id'] => Array // <---- $_SESSION['post']['edu']["'id'"]
I can't tell why this wouldn't work
session_start(); $_SESSION['post'] = $_POST;
On the 2nd page, do you call
session_start() before accessing
You could place this directly under
session_start() to help debug.
You can use serialize () to convert the array to a string and either pass it by post or store it in the session.
I've noticed funny behaviour before in saving an array to $_SESSION in PHP. Sometimes if you make a reference and use that it's easier, for example:
$session =& $_SESSION['post']; $session = array ();
Don't ask why though :)
Also, to expand on what haim evgi said in the comment, wrap the print_r in
<pre> tags such as:
echo "<pre>"; print_r ($array); echo "</pre>";
to make the array output readable.
DCD is on the right track.
Generally, I would store a serialized version of the form in the session and then unserialize it when I needed to use it.
To store it:
$_SESSION['post'] = serialize($_POST);
To retrieve it:
$post = unserialize( $_SESSION['post'] );
If you use
echo in arrays, PHP will print "Array" instead of the array contents. Use
Your assignment only looks like it its by reference. Internally (to preserve memory) it is a reference until you change
$_SESSION['post']. From that point onward, you will have two separate variables.