2014-09-30 84 views
1

我將一個數組的值從一個php文件發送到另一個php,並且我正在使用一個隱藏有seriaize的輸入,它工作正常,但是我在網頁上閱讀我可以使用序列化,當數組沒有很多的值,只是當它有幾個值,我不知道,例如數組[10]或[8],我用這個數組來保存很多值,如40或更多,我想確保序列化()將正常工作,你覺得呢?,我可以使用數組中的很多值序列化?

我的代碼在這裏:

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

就像我之前說的,這只是罰款我只是想知道你對這個觀點,因爲我的網頁,我可以使用序列化時,我必須節省閱讀幾個值

+1

這也可以保存爲一個會議,我猜? – 2014-09-30 06:18:54

+0

你會怎麼做那個朋友? – 2014-09-30 06:31:15

回答

0

我做的第一件事是不是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'],因爲它們已經在陣列。

+0

偉大的夥伴,你認爲serialize()在大數組上工作正常嗎?,有很多值的數組? – 2014-09-30 06:30:04

+0

我想不出爲什麼它不會,但我覺得使用標量值會更好,而且不必在客戶端和服務器端解析/解碼/非序列化數據 – Phil 2014-09-30 06:31:39

1

你可以把它轉換成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']); 
+0

好朋友,我可以使用json來處理大數組嗎?,有很多值的數組? – 2014-09-30 06:27:50

相關問題