更改数组中一行的值

时间:2009-10-27 06:28:44

标签: php arrays

我需要将数组中的一行的值更改为给定的字符串,然后内爆并保存数据。我使用下面的代码。

是表格的一行 目标是我要更新的数组中的特定行 nfv 是我想要放入数组的新字符串。

<?
$rowpre = $_GET['row'];
$newfieldvalue = $_GET['nfv'];
$row = --$rowpre;
$data = file_get_contents("temp.php");
$csvpre = explode("###", $data);
$i = 0;
    foreach ( $csvpre AS $key => $value){
        $i++;
        if($i = $row){
            $info = explode("%%", $value);
            $j = 0;
                foreach ( $info as $key => $value ){ 
                    $j++;
                    if($j == $target){
                        /*change the value of this line to $newfieldvalue*/
                    }
                }   
        }           
    }

$presave = implode("%%", $info);
$save = implode("###", $presave);
$fh = fopen("temp.php", 'w') or die("can't open file");
fwrite($fh, $save);
fclose($fh);
?>

1 个答案:

答案 0 :(得分:2)

您是否意识到您可以索引到数组中?如果您已经拥有数组元素的数字索引,请继续更改它:

$arr[$index] = "some new stuff";

神奇地更新。

相关问题