PHP nested array in $_SESSION <p>I'm trying to do a simple nested array in a session variable. But I'm having a hard time wrapping my head around the logic for the dynamic creation of the arrays.</p> <p>What I think my code should look like (which I know is wrong, because I want it to be dynamic):</p> <p>Page 1:</p> <pre><code>session_start(); $_SESSION['test'] = array(); </code></pre> <p>Page 2: </p> <pre><code>session_start(); $_SESSION['test'][0] = array('name' =&gt; 'john smith', 'age' =&gt; '20', 'city' =&gt; 'new york'); $_SESSION['test'][1] = array('name' =&gt; 'jane doe', 'age' =&gt; '42', 'city' =&gt; 'seattle'); </code></pre> <p>I want to be able to do a foreach loop to grab the values</p> <pre><code>foreach($_SESSION['test'] as $test){ echo "Name " . $test['name']; echo "Age " . $test['age']; echo "City " . $test['city']; } </code></pre>