使用原始csv数据打开文件,分解数组和子数组,更改行,并将其打包并保存在PHP中
我在这里的目标是打开temp.php,按#####(行终止符)分解,然后按%%(字段终止符)分解。更改特定行上的特定字段,然后将所有内容重新组合并保存 这里有几个变量:使用原始csv数据打开文件,分解数组和子数组,更改行,并将其打包并保存在PHP中,php,arrays,Php,Arrays,我在这里的目标是打开temp.php,按#####(行终止符)分解,然后按%%(字段终止符)分解。更改特定行上的特定字段,然后将所有内容重新组合并保存 这里有几个变量: 行=目标行号 target=目标字段/列编号 nfv=我要放入目标字段的信息 我使用计数器$i计数,直到到达所需的行。然后计数器$j计数,直到到达所需的字段/目标。到目前为止,这给了我一个无效内爆参数的错误,或者根本不保存任何数据。我肯定有几件事不对,但我感到沮丧和失落 <? $row = $_GET['row']; $n
行
=目标行号target
=目标字段/列编号nfv
=我要放入目标字段的信息
我使用计数器$i
计数,直到到达所需的行。然后计数器$j
计数,直到到达所需的字段/目标。到目前为止,这给了我一个无效内爆参数的错误,或者根本不保存任何数据。我肯定有几件事不对,但我感到沮丧和失落
<?
$row = $_GET['row'];
$nfv = $_GET['nfv'];
$target = $_GET['target'];
$data = file_get_contents("temp.php");
$csvpre = explode("###", $data);
$i = 0;
$j = 0;
foreach ( $csvpre AS $key => $value){
$i++;
if($i == $row){
$info = explode("%%", $value);
foreach ( $info as $key => $value ){
$j++;
if($j == "$target"){
$value = $nfv;
}
}
$csvpre[$key] = implode("%%", $info);
}
}
$save = implode("###", $csvpre);
$fh = fopen("temp.php", 'w') or die("can't open file");
fwrite($fh, $save);
fclose($fh);
header("Location: data.php");
?>
以下几点应该行得通。这也消除了许多不必要的循环
<?php
$row = $_GET['row'];
$nfv = $_GET['nfv'];
$target = $_GET['target'];
$data = file_get_contents("temp.php");
$csvpre = explode("###", $data);
//removed i and j. unnecessary.
//checks if the row exists. simple precaution
if (isset($csvpre[$row]))
{
//temporary variable, $target_row. just for readability.
$target_row = $csvpre[$row];
$info = explode("%%", $target_row);
//check if the target field exists. just another precaution.
if (isset($info[$target]))
{
$info[$target] = $nfv;
}
//same as yours. just pack it back together.
$csvpre[$row] = implode("%%", $info);
}
$save = implode("###", $csvpre);
$fh = fopen("temp.php", 'w') or die("can't open file");
fwrite($fh, $save);
fclose($fh);
header("Location: data.php");
?>
你还没有问过这个问题吗?我忍不住想知道。。。如果您知道这些值将有%%分隔符,为什么不做一个简单的str_替换(“%%$target%%”,“%%$nfv%%,$file_作为字符串”)
?@anthony,$target是字段号,而不是字段中的值。在我尝试此操作时,您能否详细说明一下您所做的事情以及原因?顺便说一句,感谢您花费时间。我尝试了此操作,结果是我选择了第1行和我的目标。它运行成功,但它创建了一个空行,并且没有更新指定的行/目标。Looks喜欢把六个字母排成一行。但是我也没有在任何地方看到我的更新信息(nfv的值)。我在$info=explode(%%,$value);
我在编辑中更正了它。请确保你的上面写着$info=explode(%%,$target\u行)
我刚刚在PHP5.2.9上进行了测试,可以确认它是否正常工作。有关数据,请参阅,对于上面的文件(减去位置更改),它正常工作!非常感谢您花时间解释这里发生的事情。
<?php
$row = $_GET['row'];
$nfv = $_GET['nfv'];
$target = $_GET['target'];
$data = file_get_contents("temp.php");
$csvpre = explode("###", $data);
//removed i and j. unnecessary.
//checks if the row exists. simple precaution
if (isset($csvpre[$row]))
{
//temporary variable, $target_row. just for readability.
$target_row = $csvpre[$row];
$info = explode("%%", $target_row);
//check if the target field exists. just another precaution.
if (isset($info[$target]))
{
$info[$target] = $nfv;
}
//same as yours. just pack it back together.
$csvpre[$row] = implode("%%", $info);
}
$save = implode("###", $csvpre);
$fh = fopen("temp.php", 'w') or die("can't open file");
fwrite($fh, $save);
fclose($fh);
header("Location: data.php");
?>