Copy a multi-dimentional array by Value (not by reference) in PHP.
Posted
by Simon R
on Stack Overflow
See other posts from Stack Overflow
or by Simon R
Published on 2010-03-25T10:28:15Z
Indexed on
2010/03/25
10:43 UTC
Read the original article
Hit count: 332
Language: PHP
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_start();
$_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:
Array ( [formid] => 2 [edu] => Array ( ['id'] => Array ( [1] => new_1 [2] => new_2 ) ['nameOfInstitution'] => Array ( [1] => 1 [2] => 2 ) ['qualification'] => Array ( [1] => blah [2] => blah ) ['grade'] => Array ( [1] => blah [2] => 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?
© Stack Overflow or respective owner