Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中更改数组变量值中的变量值_Php_Arrays_If Statement_While Loop - Fatal编程技术网

在php中更改数组变量值中的变量值

在php中更改数组变量值中的变量值,php,arrays,if-statement,while-loop,Php,Arrays,If Statement,While Loop,我试图更新数组变量值中的变量值 您将看到我正在编写一个包含以下内容的文件:file\u put\u contents()。内爆(“\r\n”,$contents).包含$contents变量 如果($I==$per\u文件){… 很明显,$contents数组在本例中无法更新变量值$body\u file\u count $body\u file\u count是输出的文件数。它实际上与文件标题中的数字相同:$file\u count 基本上,我只需要将$body\u file\u count写

我试图更新数组变量值中的变量值

您将看到我正在编写一个包含以下内容的文件:
file\u put\u contents()
内爆(“\r\n”,$contents).
包含
$contents
变量

如果($I==$per\u文件){…

很明显,$contents数组在本例中无法更新变量值
$body\u file\u count

$body\u file\u count
是输出的文件数。它实际上与文件标题中的数字相同:
$file\u count

基本上,我只需要将
$body\u file\u count
写入:

$default_contents=$contents=array("BODY CONTENT TOP . "$body_file_count" . ");
在每个if
($i==$per_file){
迭代中。显然,如果我可以将$file_计数传递给
$content
,因为
$file_计数
正在按预期更新标题,我就可以废弃
$body_file_计数

$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()

首先要注意,您忘记在$default\u contents initialization上添加字符串连接运算符(“.”)

我不知道我是否理解您的问题。如果我理解您的问题,您可以尝试在每次更改$body\u file\u计数时更新$default\u内容++



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




我想我已经尝试过了,但是我没有在if迭代结束时放置$contents。它现在似乎正在输出$file\u计数。谢谢你的建议。
$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()