【问题标题】:Change a value of a variable within an array variable value in php在php中更改数组变量值中的变量值
【发布时间】:2013-06-28 06:13:16
【问题描述】:

我正在尝试更新数组变量值中的变量值。

你会看到我正在写出一个文件:file_put_contents()。 implode("\r\n", $contents)... 包含 $contents 变量。

我需要$body_file_count 来增加if ($i == $per_file) {... 的每次迭代

很明显 $contents 数组在这种情况下无法更新变量值$body_file_count。

$body_file_count 是输出的文件数。文件标题中其实是同一个数字:$file_count...

基本上,我只需要将$body_file_count 写到:

$default_contents=$contents=array("BODY CONTENT TOP . "$body_file_count" . ");

每个 if ($i == $per_file) { 迭代。显然,如果我可以将 $file_count 传递给$content,我可以废弃$body_file_count,因为$file_count 正在按预期更新标题。

$body_file_count = 0;
$footer = "FOOTER";
$default_contents = $contents = array("BODY CONTENT TOP . "$body_file_count" . ");

while ($row = mysql_fetch_array($result)) {
    $line = "...";
    $contents[] = $line; // Each array element will be a line in the text file
    $i++;
    $recs++;
    if ($i == $per_file) {
        $contents[] = $footer; // Add the footer to the end
        file_put_contents($_POST["a"] . "-#" . $file_count .  "-" . date('Y') . "-" . $_POST["b"] . "-" . $recs . "-" . $txtdate .  '.txt', implode("\r\n", $contents));
        $i = 0;
        $recs = 0;
        $contents = $default_contents;
        $file_count++;
        $body_file_count++;
    } // End of if()
} // End of while()

【问题讨论】:

    标签: php arrays if-statement while-loop


    【解决方案1】:

    首先请注意,您忘记在 $default_contents 初始化时添加字符串连接运算符 (".")

    我不知道我是否理解你的问题。如果我很了解您的问题,您可以尝试在每次更改 $body_file_count++ 时更新 $default_contents


    $body_file_count = 0;
    $footer = "FOOTER";
    $default_contents = $contents = array("BODY CONTENT TOP . " . $body_file_count . " . ");
    
    while ($row = mysql_fetch_array($result)) {
        $line = "...";
        $contents[] = $line; // Each array element will be a line in the text file
        $i++;
        $recs++;
        if ($i == $per_file) {
            $contents[] = $footer; // Add the footer to the end
            file_put_contents($_POST["a"] . "-#" . $file_count .  "-" . date('Y') . "-" . $_POST["b"] . "-" . $recs . "-" . $txtdate .  '.txt', implode("\r\n", $contents));
            $i = 0;
            $recs = 0;
            $file_count++;
            $body_file_count++;
            $default_contents = array("BODY CONTENT TOP . " . $body_file_count . " . ");
            $contents = $default_contents;
        } // End of if()
    } // End of while()
    

    另外,如果除了提供初始内容之外您不需要此变量的任何其他内容,那么您可以将其拿走


    $body_file_count = 0;
    $footer = "FOOTER";
    $contents = array("BODY CONTENT TOP . " . $body_file_count . " . ");
    
    while ($row = mysql_fetch_array($result)) {
        $line = "...";
        $contents[] = $line; // Each array element will be a line in the text file
        $i++;
        $recs++;
        if ($i == $per_file) {
            $contents[] = $footer; // Add the footer to the end
            file_put_contents($_POST["a"] . "-#" . $file_count .  "-" . date('Y') . "-" . $_POST["b"] . "-" . $recs . "-" . $txtdate .  '.txt', implode("\r\n", $contents));
            $i = 0;
            $recs = 0;
            $file_count++;
            $body_file_count++;
            $contents = array("BODY CONTENT TOP . " . $body_file_count . " . ");
        } // End of if()
    } // End of while()
    

    【讨论】:

    • 我以为我已经尝试过了,但我没有将 $contents 放在 if 迭代的末尾。它现在似乎正在输出 $file_count OK。谢谢你的建议。
    猜你喜欢
    • 2013-05-08
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    相关资源
    最近更新 更多