我正在尝试将简短形式的结果存储到平面文件(例如 .text 文件)中。该文件将作为分隔文件导入 Excel。该表单具有复选框,因此可以将其设置为数组:
<input type="hidden" name="roflz" value="noresponse">
<input type="checkbox" name="roflz[]" value="Yesroflz" id="Yesroflz" <?php if (isset($_SESSION['roflz']) && $_SESSION['rolfz'] == "Yesroflz") { echo 'checked="checked"'; } ?>> <label for="Yesroflz">Yesroflz</label> <br />
<input type="checkbox" name="roflz[]" value="Moreroflz" id="Moreroflz" <?php if (isset($_SESSION['roflz']) && $_SESSION['roflz'] == "Moreroflz") { echo 'checked="checked"'; } ?>> <label for="Moreroflz">Moreroflz</label><br />
是否可以从 内爆一个数组$_POST['rolfz']
并将结果返回到$_POST['rolfz']
?所需的结果是我可以存储到文本文件中的字符串。请参见下面的代码:
<?php
// begin the session
ini_set('session.cache_limiter', 'private');
session_start();
//Turn checkboxes (an array) into a string. So we can later store it into a strong.
if(isset($_POST['rolfz'])){
$_POST['rolfz'] = implode(",",$_POST['rolfz']);
}
总体目标是将 $_SESSION 中的所有值存储到文本文件中。但是,当我尝试运行代码时,我得到“注意:第 14 行 E:\cgi-bin\thankyou.php 中的数组到字符串转换”。错误告诉我,在下面的代码中,我试图将数组用作字符串。
// Take each input name and create a variable for it
foreach($_POST as $k=>$v) {
$_SESSION[$k]=$v;
}
//Implodes the array so we can store the values as a string (not the variables name though!)
$results = implode("|", $_SESSION);
//Open up the file that we will store the collected information to
$datafile=fopen("V1.txt","a") or exit("Unable to open file to record data!");
//Write the data on a new line ("\r\n") so we don't over-write the existing data
fwrite($datafile, "\r\n".$results);
//Close out the file
fclose($datafile);
//reset session variables and destroy the session
$_SESSION = array();
session_destroy();
?>
如果我正在做的事情是错误的或不可能的,是否可以使用替代语句代替,foreach($_POST as $k=>$v)
以便它忽略$_POST['rolfz']
?这样我可以$_POST['rolfz']
自己处理吗?
编辑(5/10/11):内爆 前:
array(5) { ["date"]=> string(10) "05/10/2011" ["time"]=> string(11) "09:11:20 AM" ["gender"]=> string(4) "male" ["lolz"]=> string(7) "YesLOLZ" ["roflz"]=> array(2) { [0]=> string(8) "Yesroflz" [1]=> string(7) "Noroflz" } }
爆破后:
array(5) { ["date"]=> string(10) "05/10/2011" ["time"]=> string(11) "09:11:20 AM" ["gender"]=> string(4) "male" ["lolz"]=> string(7) "YesLOLZ" ["roflz"]=> array(2) { [0]=> string(8) "Yesroflz" [1]=> string(7) "Noroflz" } }
编辑(2011 年 5 月 10 日): 如果您尝试执行类似的解决方案,请参阅 Michael 的解决方案。请注意,我是个白痴,在我的代码中使用了错误的复选框名称(应该是 roflz 而不是 rolfz)。