2009-10-27 64 views
0

我需要將數組中某一行的值更改爲給定字符串,然後分解並保存數據。我使用下面的代碼。更改數組中某一行的值

是該表的行。
目標是我想要更新的數組中的特定行。
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); 
?> 

回答

2

你一定要明白,你可以索引到一個數組?如果你已經有一個數組元素的數字索引,先走一步,改變它:

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

奇妙的更新。

+0

所以在我爆炸信息後,我可以做一個$ info [$ target] =「新東西」; ? – mrpatg 2009-10-27 06:40:46

+0

你可以。 * Iff *我明白你的代碼正確。它並沒有在「告訴我你在做什麼」的規模上居於高位。 – Joey 2009-10-27 06:54:27