dtwy2858 2013-07-01 07:12
浏览 62
已采纳

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

I am trying to update a variable value that is within an array variable value.

You will see I am writing out a file with: file_put_contents(). The implode(" ", $contents)... contains the $contents variable.

I need $body_file_count to increment every iteration of the if ($i == $per_file) {...

It's pretty evident the $contents array cannot update a variable value in this case $body_file_count.

$body_file_count is the number of files outputted. It's actually the same number in the file title: $file_count...

Basically, I just need to write the $body_file_count to the:

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

on each if ($i == $per_file) { iteration. Obviously I could scrap $body_file_count if I could pass $file_count to the $content as $file_count is updating the title as expected.

$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("
", $contents));
        $i = 0;
        $recs = 0;
        $contents = $default_contents;
        $file_count++;
        $body_file_count++;
    } // End of if()
} // End of while()
  • 写回答

1条回答 默认 最新

  • dongzhan5286 2013-07-01 08:28
    关注

    First beware that you have forget to add the string concatenation operator (".") on the $default_contents initialitation

    I don't know if i have understand well your question. If i have understand well your problem you can try to update the the $default_contents everytime that you change $body_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("
    ", $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()
    

    Also if you don't need for anything else this variable besides to provide an initial content then you can just take it away


    $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("
    ", $contents));
            $i = 0;
            $recs = 0;
            $file_count++;
            $body_file_count++;
            $contents = array("BODY CONTENT TOP . " . $body_file_count . " . ");
        } // End of if()
    } // End of while()
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)