Passing arrays in PHP (implode/explode)

They have: 105 posts

Joined: Jan 2002

So, for days I searched for a solution to pass arrays to multiple pages in php and have come up with this 'solution'

I first implode the array to turn it into a string, then I attach the string name to the script I'm sending it to. Inside of that particular script, I explode the string and have my array again. It's so simple, I'm sure this is a requote of another genius. It's just that I couldn't find the solution anywhere.

In original file:

$new_string = implode(" ", $array);
Sending my array

in newscript.php:

explode(" ", $array);

CPRhosting.com - CPR Hosting...Giving life to the web
Custom-made Hosting Plans starting at $2

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

As long as there are no spaces in your array elements, and as long as the string doesn't get too long that will work just fine.

Another option is to serialize the array:

<?php
// taken from PHP.net unserialize() User Contributed Notes
// PAGE 1:
$array=rawurlencode(serialize($array));

// PAGE 2:
$array=unserialize(urldecode(stripslashes($array)));
?>

PHP Docs: serialize(), unserialize()

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.