2

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.

1
  • 2
    You should print_r those to see what's there. Commented May 18, 2010 at 17:52

4 Answers 4

7

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I'll have to nitpick. PHP has nested arrays, not really multidimensional arrays.
6

As noted, try

echo "<pre>";
print_r($_SESSION);
echo "</pre>";

That should show you what's in the session array.

2 Comments

What does the output from the print_r show? That may help us with finding the issue.
print_r is not to put into your page (as stated by jordanstephens in his answer). It's for debugging, so we can see the structure of the array.
0

A 2-dimensional table is just an array of arrays.

So, by pulling out $_SESSION["table"][0], you're pulling out an array that represents the first row of the table.

If you want a specific value from that table, you need to pass the second index, too. i.e. $_SESSION["table"][0][0]

Or you could just be lazy and do $table = $_SESSION["table"]; at which point $table would be your normal table again.

Comments

0

A nice way ...

<?php
    foreach ($_SESSION as $key => $value) {
        echo $key . " => " . $value . "<br>";
    }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.