How to pass arrays from one document to the other?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TheDumbNerd
Forum Newbie
Posts: 13
Joined: Sun Nov 13, 2011 4:01 pm

How to pass arrays from one document to the other?

Post by TheDumbNerd »

If i had a value that the user had entered into my form on a form page, how do you pass that value onto another page where it is dispayed in a table? Like it gets validated on one page, lets called it the process page. If the values that the user hadentered into the form are legal, then you get redirected to a main page where thedata that the userJUST ENTERED is displayed in a table.
Livengood
Forum Newbie
Posts: 11
Joined: Tue Nov 01, 2011 2:48 pm

Re: How to pass arrays from one document to the other?

Post by Livengood »

You load into a string and put each array in it together with a : and then split the : back into an array.
Hermit TL
Forum Commoner
Posts: 69
Joined: Mon Nov 21, 2011 12:16 am

Re: How to pass arrays from one document to the other?

Post by Hermit TL »

Someone correct me if I'm wrong (as I have never tried it) but can't an array be stored in $_SESSION['value']?
I use $_SESSION for passing single variables between scripts but I thought it could be used for arrays as well.
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: How to pass arrays from one document to the other?

Post by mikeashfield »

Code: Select all

<?php
// begin the session
session_start();
 
// create an array
$array_names=array('mike', 'sarah', 'jake', 'liam');
 
// put the array in a session variable
$_SESSION['names']=$array_names;
 
// a little message to say we have done it
echo 'Putting array into a session variable';
?>
Now that you have the array $array_names in a session variable called $_SESSION['names'] we can have a look through the array as we choose.

Code: Select all

<?php
// begin the session
session_start();
 
// loop through the session array with foreach
foreach($_SESSION['names'] as $key=>$value)
    {
    // and print out the values
    echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
    }
?>
Post Reply