PHP - How to Send an Array to Another Page

PHP - How to send an array to another page?

You could put it in the session:

session_start();
$_SESSION['array_name'] = $array_name;

Or if you want to send it via a form you can serialize it:

<input type='hidden' name='input_name' value="<?php echo htmlentities(serialize($array_name)); ?>" />

$passed_array = unserialize($_POST['input_name']);

The session has the advantage that the client doesn't see it (therefore can't tamper with it) and it's faster if the array is large. The disadvantage is it could get confused if the user has multiple tabs open.

Edit: a lot of answers are suggesting using name="input_name[]". This won't work in the general case - it would need to be modified to support associative arrays, and modified a lot to support multidimensional arrays (icky!). Much better to stick to serialize.

How to pass an array to another page with PHP?

You could put it in the session:

session_start();
$_SESSION['array_name'] = $array_name;

Or if you want to send it via a form you can serialize it:

<input type='hidden' name='input_name' value="<?php echo htmlentities(serialize($array_name)); ?>" />

$passed_array = unserialize($_POST['input_name']);

Passing an array to another page (Form)

i'm glad @ksealey pointed out a more proper method of doing this, but for the sake of answering the question...the reason it's not working is that the serialize alone is not enough to prevent the invalid html. see result of what the serialize leaves in the html:

Sample Image

so you need to be sure the html you produce is valid. you might use encoding like base64 to produce safe html:

echo  " <form id=\"my_form\" action=\"\" method=\"post\"";
echo "enctype=\"multipart/form-data\">";
echo "<input type=\"hidden\" name=\"id\" value=\"10\">";
echo "<input type=\"hidden\" name=\"input_name\" ";

echo "value=\"".base64_encode(serialize($my_array))."\" />";

Sample Image

then you can just add the decode to your output:

$passed_array = unserialize(base64_decode($_POST['input_name']));
print_r($passed_array);

How to pass array to another page by using anchor in php

This is clear example code

first.php

<?php
$Mixed = array("1","2","3");
$Text = json_encode($Mixed);
$RequestText = urlencode($Text);
?>
<a href="second.php?cluster=<?php echo $RequestText; ?>">Click</a>

second.php

<?php
$Text = urldecode($_REQUEST['cluster']);
$Mixed = json_decode($Text);
print_r( $Mixed);
?>

I have checked, and it is working fine.

Transfer array from one page to another using CURL

OK, the bottom line of the discussion in the comments above leads to this result:

The sending part:

<?php
$url = 'http://localhost/out.php';
$myvars = array("one","two","three");
$post_elements = array('myvars'=>$myvars);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($post_elements));
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
print_r($response);

The receiving part:

<?php
print_r($_POST);

The output on the sending side is:

Array ( [myvars] => Array ( [0] => one [1] => two [2] => three ) )

which basically says that you can simply use $_POST['myvars'] on the receiving side which will exactly hold the scalar array you want to transfer.



Related Topics



Leave a reply



Submit