2013-07-01 2 views
0

Я пытаюсь обновить значение переменной, которое находится внутри значения переменной массива.Измените значение переменной в значении переменной массива в php

Вы увидите, что я пишу файл с: file_put_contents(). implode("\r\n", $contents)... содержит переменную $contents.

мне нужно $body_file_count приращение каждой итерации if ($i == $per_file) {...

Это довольно очевидно, содержимое массива $ не может обновить значение переменной в этом случае $body_file_count.

$body_file_count - количество выводимых файлов. Это фактически тот же номер в названии файла: $file_count ...

В принципе, мне просто нужно написать $body_file_count к:

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

на каждой итерации, если ($i == $per_file) {. Очевидно, я мог бы отказаться от $body_file_count, если бы смог передать $ file_count на $content, так как $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() 

ответ

1

Сначала нужно учитывать, что вы забыли добавить оператор конкатенации («») на $ default_contents initialitation

Я не знаю, если у меня есть хорошо понимаю ваш вопрос. Если у меня есть хорошо понять вашу проблему, вы можете попробовать обновить за $ default_contents каждый раз, что вы меняете $ 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("\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() 

+0

Я думал, что пробовал это уже, но я не размещал содержимое $ в конце if-итерации. Похоже, что теперь он выводит значение $ file_count. Спасибо за предложение. – OldWest

Смежные вопросы