PHP session array value keeps showing as "Array"
When submitting data from the form to the second page, the session value always matches the name "Array" with the expected number.
The data should be displayed in a table, but enter the example 1, 2, 3, 4 i: Array, Array, Array. (2-dimensional table used)
Is the following code below a suitable way to "call" the stored values on the 2nd page from an array?
$test1 = $_SESSION["table"][0];
$test2 = $_SESSION["table"][1];
$test3 = $_SESSION["table"][2];
$test4 = $_SESSION["table"][3];
$test5 = $_SESSION["table"][4];
What is it and how can I fix it? Is this some kind of override that needs to happen?
Best wishes.
a source to share
You don't need any override. The script prints "Array" and not the value because you are trying to print the whole array to the screen and not the value in the array, like so:
$some_array = array('0','1','2','3');
echo $some_array; //this will print out "Array"
echo $some_array[0]; //this will print "0"
print_r($some_array); //this will list all values within the array. Try it out!
print_r()
not useful for production code because it is ugly; however, for testing purposes, it may prevent you from pulling your hair out behind nested arrays.
This is great for accessing the elements in your array by index: $some_array[2]
if you want it in a table you can do something like this:
<table>
<tr>
for($i = 0 ; $i < count($some_array) ; $i++) {
echo '<td>'.$some_array[$i].'</td>';
}
</tr>
</table>
a source to share
A 2D table is just an array of arrays.
So by pulling out $_SESSION["table"][0]
you are pulling out an array that represents the first row of the table.
If you want to get a specific value from this table, you need to pass the second index as well. those.$_SESSION["table"][0][0]
Or you could just be lazy and do $table = $_SESSION["table"];
, at that point it $table
will be your regular table again.
a source to share