Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Session Array Value keeps showing as "Array"

Tags:

php

When sending data from a form to a second page, the value of the session is always with the name "Array" insteed of the expected number.

The data should get displayed in a table, but insteed of example 1, 2, 3 , 4 i get : Array, Array, Array. (A 2-Dimensional Table is used)

Is the following code below a proper way to "call" upon the stored values on the 2nd page from the array ?

$test1 = $_SESSION["table"][0];
$test2 = $_SESSION["table"][1];
$test3 = $_SESSION["table"][2];
$test4 = $_SESSION["table"][3];
$test5 = $_SESSION["table"][4];

What exactly is this, and how can i fix this? Is it some sort of override that needs to happen?

Best Regards.

like image 921
Nerathas Avatar asked Jan 22 '26 04:01

Nerathas


1 Answers

You don't need any sort of override. The script is printing "Array" rather than a value, because you're trying to print to the screen a whole array, rather than a value within an array for example:

$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() is not useful for production code, because its ugly; however, for testing purposes it can keep you from pulling your hair out over nested arrays.

It's perfectly fine to access elements in your array by index: $some_array[2]

if you want it in a table you might do something like this:

<table>
    <tr>
    for($i = 0 ; $i < count($some_array) ; $i++) {
        echo '<td>'.$some_array[$i].'</td>';
    }
    </tr>
</table>
like image 102
bimbom22 Avatar answered Jan 24 '26 19:01

bimbom22



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!