【问题标题】:change value of a line in an array更改数组中一行的值
【发布时间】:2009-10-27 06:28:44
【问题描述】:

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

row 是表格的行。
target 是数组中我要更新的特定行。
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);
?>

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    您确实意识到您可以对数组进行索引?如果您已经拥有数组元素的数字索引,请继续更改它:

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

    神奇更新。

    【讨论】:

    • 所以在我爆炸信息之后,我可以做一个 $info[$target] = "new stuff"; ?
    • 你可以。 如果我理解你的代码是正确的。它实际上并没有在“告诉我你做了什么”规模上排名很高。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 2012-09-11
    • 2014-03-15
    相关资源
    最近更新 更多