使用隐藏的输入发送php$Array


Sending a php $Array using an input hidden

我将php中的数组的值从一个php文件发送到另一个文件,并且我使用了一个用seriaize隐藏的输入,它工作得很好,但我在网页上读到,当数组没有很多值时,我可以使用serialize,只是当它有几个值,我不知道,例如数组[10]或[8],我使用这个数组保存了很多值,比如40或更多,我想确保serialize()在这方面能很好地工作,你觉得呢?,我可以对数组中的许多值使用serialize吗?

我在这里的代码:

////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']);

就像我之前说的,它很好用,我只想知道你对此的看法,因为我在网页上读到,当我必须保存一些值

时,我可以使用序列化

您可以将其转换为json格式并发送,也可以再次对其进行解码以获得相同的数组

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']);

我要做的第一件事是不echo输出大块的静态HTML。

我要做的第二件事是。。。

<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>

然后,当您访问$_POST['code_period_name']$_POST['selection']时,它们将已经是数组。