1

i sending the values of an Array in php from a php file to another, and i am using an input hidden with seriaize, it works fine, but i read on a web page that i can use serialize when the array has not a lot of values just when it has a few values, i dont know, for example an array[10] or [8], and I using this array to save a lot of values, like 40 or more, and i want to make sure that serialize() will work fine on that, what do you think?, can I use serialize with a lot of values inside an array?

My code here:

////file1.php////

//here both $code_period and $selection are array, and i dont know exactly how many values i will
//save inside them   

echo "<form name='formprocess' method='post' action='process.php'>

<input name='code_period_name' type='hidden' value='".serialize($code_period)."'>
<input name='selection' type='hidden' value='".serialize($selection)."'>

<input style='background:#13284B;color:White' type='submit' value='Process'>

</form>";



////process.php////

$code_period_name=unserialize($_POST['code_period_name']); //im catching these values this way
$selection=unserialize($_POST['selection']);

Like I said before, It works just fine I just wanna know your opinion about this, cause i read on a web page that i can use serialize when i have to save a few values

2
  • 1
    This can also be saved as a session I guess? Commented Sep 30, 2014 at 6:18
  • How would you do that buddy? Commented Sep 30, 2014 at 6:31

2 Answers 2

1

You can convert it to json format and send it you can again decode it back to get the same array

echo "<form name='formprocess' method='post' action='process.php'>

<input name='code_period_name' type='hidden' value='".json_encode($code_period)."'>
<input name='selection' type='hidden' value='".json_encode($selection)."'>

<input style='background:#13284B;color:White' type='submit' value='Process'>

</form>";

In Process.php
$code_period_name=json_decode($_POST['code_period_name']); //im catching these values this way
$selection=json_decode($_POST['selection']);
Sign up to request clarification or add additional context in comments.

1 Comment

Great buddy, Can i use json to work with big arrays?, arrays with a lot of values?
0

First thing I'd do is not echo out huge chunks of static HTML.

The second thing I'd do is the following...

<form name="formprocess" method="post" action="process.php">

<?php foreach ($code_period as $val) : ?>
<input type="hidden" name="code_period_name[]" value="<?= htmlspecialchars($val) ?>">
<?php endforeach ?>

<?php foreach ($selection as $val) : ?>
<input type="hidden" name="selection[]" value="<?= htmlspecialchars($val) ?>">
<?php endforeach ?>

<button type="submit">Process</button>

</form>

Then, when you access $_POST['code_period_name'] or $_POST['selection'], they will already be arrays.

2 Comments

Great buddy, Do you think serialize() works fine on big arrays?, arrays with a lot of values?
I can't think of any reason why it wouldn't but I feel it would be better to work with scalar values and not have to parse / decode / unserialise data at both client and server ends

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.