tags:

views:

32

answers:

1

I need to change the value of a line within an array to a given string, then implode and save the data. Im using the code below.

row is the row of the table.
target is the specific line in the array which i want to update.
nfv is the new string i want to put into the array.

<?
$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);
?>
+2  A: 

You do realize that you can index into an array? If you already have the numeric index of an array element, just go ahead and change it:

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

Magically updated.

Joey
so after i explode info, i could do a $info[$target] = "new stuff"; ?
Patrick
You could. *Iff* I understood your code right. It doesn't actually rank high on the "tell me what you do" scale.
Joey