I have an outer array with 2 elements inside. Each of the 2 elements is an array. So it's a 2-dimensional array.
Here is how the array is created:
$outerArray = array();
$nestedArray = array("first", "second", "third", "fourth");
$outerArray[] = $nestedArray;
$nestedArray_2 = array("first", "second", "third", "fourth");
$outerArray[] = $nestedArray_2;
I then store this array as follows:
$jsonArray = json_encode($outerArray);
// USE 'echo' TO PUT THIS array INTO A HIDDEN input FIELD ON THE PAGE
echo "document.getElementById('jsonArray').value = $jsonArray;";
// LETS ECHO THE NESTED ARRAY TO VERIFY IT WAS CONSTRUCTED CORRECTLY
echo $jsonArray;
Here is the HTML hidden element 'jsonArray' in my file:
<input type="hidden" id="jsonArray" />
That echo I did just above here shows me the array looks like this:
[["first","second","third","fourth"],["first","second","third","fourth"]]
All this is good. Right now it's clear that the hidden input field has a valid 2D json array stored in it.
I then do the following with the array -- I store it in the session, redirect to another file, which extracts then echoes the 2D array:
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// SEE NOTE #1 BELOW ABOUT WHAT THIS SHOWS IN THE ALERT() BOX
alert("The xmlhttp.responseText is: " + xmlhttp.responseText);
window.location = "theViewer.php";
}
}
var arg = "theStored2dimensionArrayAsJson="
+ document.getElementById('jsonArray').value;
xmlhttp.open("POST", "handler.php", true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send(arg);
So what this does, it sends the json-encoded 2-dimensional array to handler.php. And all handler.php does is save the json-encoded 2-dimension array in the $_SESSION:
// in handler.php
$_SESSION['jsonEncoded2D_Array'] = $_POST['theStored2dimensionArrayAsJson'];
// echo out the array so it appears in the xmlhttp.responseText
echo $_POST['theStored2dimensionArrayAsJson'];
// NOTE #1 -- THIS IS WHAT IS DISPLAYED IN THE xmlhttp.responseText
handler.php, the jsonEncoded2D_Array is:
first,second,third,fourth,first,second,third,fourth
After displaying the xmlhttp.responseText in an alert() box, I redirect to theViewer.php. I retrieve the 2D array from the SESSION and echo it out:
// INSIDE theViewer.php
$jsonified_2D_Array = $_SESSION['jsonEncoded2D_Array'];
But when I echo out the 'array' I just retrieved from the SESSION, it's a string now, not an array:
// THE echo SHOWS THE 2D ARRAY HAS BEEN COVERTED TO A TEXT STRING
// AND IS NO LONGER AN ARRAY
echo "Okay, here is the 2D array:<br />" . $jsonified_2D_Array;
The output of the echo just above is this:
Okay, here is the 2D array:
first,second,third,fourth,first,second,third,fourth
I don't get this. If you look at the code above, I echo this array after putting it into a hidden input field and it looks correct, like a 2D array. And that is after I converted it to JSON.
Then I take that JSON-ified 2D array out of that hidden field, pass it via Ajax to handler.php in order to save in the SESSION, AND then take it back out of the SESSION in theViewer.php -- and it is no longer a 2d array -- it is now just a text string.
I need to keep this 2D array as a 2D array and not a string.
Is this possible? I need the 2D array (not a stringified array) in theViewer.php.
I tried JSON.parse() and eval() on the stringified array in theViewer.php and it stopped code execution, it did not help.