Php-在每个数组块的末尾添加一个计数器,并在末尾添加一个文本

Php-在每个数组块的末尾添加一个计数器,并在末尾添加一个文本,php,arrays,forms,Php,Arrays,Forms,在每个数组块的beggin处添加计数器和在末尾添加文本时遇到问题 下面是我的实际代码。我使用$txtcontent作为Web表单的文本输入 function hexToStr($hex) { $string=''; for ($i=0; $i < strlen($hex)-1; $i+=2) { $string .= chr(hexdec($hex[$i].$hex[$i+1])); } return $string;

在每个数组块的beggin处添加计数器和在末尾添加文本时遇到问题

下面是我的实际代码。我使用$txtcontent作为Web表单的文本输入

        function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}



    $format = $_POST['format'];
    $titre= strtoupper($_POST['titre']);
    $txtcontent = $_POST['texte'];
    $txtcontent =  wordwrap($txtcontent,20,hexToStr('0D0A'),true);


$txtcontent = explode("\n", $txtcontent);
$input = array_chunk($txtcontent, 9, false);
foreach($input as $key => $array){
    foreach($array as $k => $v){
        $input[$key][$k] = '\text '.($k*6+1).',1,'.$commas.str_replace(hexToStr('0D'), "", $v).$commas.hexToStr('0D0A');
    }
}
function array_flatten($array) { 
  if (!is_array($array)) { 
    return FALSE; 
  } 
  $result = array(); 
  foreach ($array as $key => $value) { 
    if (is_array($value)) { 
      $result = array_merge($result, array_flatten($value)); 
    } 
    else { 
      $result[$key] = $value; 
    } 
  } 
  return $result; 
} 
$array = array_flatten($input);

$filecontent = implode($array);     

    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment;filename='.$_POST['titre'].'.'.$format.'');
    header('Cache-Control: max-age=0');

    //$fh = fopen($filename, 'wb');
    $fh = fopen('php://output', 'wb');
    fwrite($fh, $filecontent);
    fclose($fh);
我将有一个包含以下内容的文件:

\text 1,1,"line1"
\text 7,1,"line2"
\text 13,1,"line3"
\text 19,1,"line4"
\text 25,1,"line5"
\text 31,1,"line6"
\text 37,1,"line7"
\text 43,1,"line8"
\text 49,1,"line9"
\text 1,1,"line10"
\text 7,1,"line11"
\text 13,1,"line12"
\text 19,1,"line13"
\text 25,1,"line14"
\text 31,1,"line15"
\text 37,1,"line16"
\text 43,1,"line17"
\text 49,1,"line18"
\text 1,1,"line19"
\text 7,1,"line20"
但是,我想要下面的代码。应该在每个块的开头添加计数器,如\If S=1。。。最后,文本\IfEnd

\If S=1
\text 1,1,"line1"
\text 7,1,"line2"
\text 13,1,"line3"
\text 19,1,"line4"
\text 25,1,"line5"
\text 31,1,"line6"
\text 37,1,"line7"
\text 43,1,"line8"
\text 49,1,"line9"
\IfEnd
\If S=2
\text 1,1,"line10"
\text 7,1,"line11"
\text 13,1,"line12"
\text 19,1,"line13"
\text 25,1,"line14"
\text 31,1,"line15"
\text 37,1,"line16"
\text 43,1,"line17"
\text 49,1,"line18"
\IfEnd
\If S=3
\text 1,1,"line19"
\text 7,1,"line20"
请注意,输入可以始终大于20行


有人对如何做有什么建议吗?

我尝试用以下代码代替foreach循环,结果得到了与您描述的相同的输出(减去引号):

我希望这有帮助。基本上,我在数组中添加了额外的键值对,以获得每次迭代的If和IfEnd,并保留了一些计数

\If S=1
\text 1,1,"line1"
\text 7,1,"line2"
\text 13,1,"line3"
\text 19,1,"line4"
\text 25,1,"line5"
\text 31,1,"line6"
\text 37,1,"line7"
\text 43,1,"line8"
\text 49,1,"line9"
\IfEnd
\If S=2
\text 1,1,"line10"
\text 7,1,"line11"
\text 13,1,"line12"
\text 19,1,"line13"
\text 25,1,"line14"
\text 31,1,"line15"
\text 37,1,"line16"
\text 43,1,"line17"
\text 49,1,"line18"
\IfEnd
\If S=3
\text 1,1,"line19"
\text 7,1,"line20"
$i = 1;
foreach($input as $key => $array){
    $j = 1;
    $input[$key][0] = "\If S=".$i."\n";
    foreach($array as $k => $v){
        $input[$key][$k+1] = '\text '.($k*6+1).',1,'.$commas.str_replace(hexToStr('0D'), "", $v).$commas.hexToStr('0D0A');
        $j++;
    }
    if ($j == 10) $input[$key][10] = "\IfEnd\n";
    $i++;
}