【问题标题】:Php - array_chunk add text according with the key value and output it to a filephp - array_chunk 根据键值添加文本并输出到文件
【发布时间】:2012-12-15 06:38:13
【问题描述】:

我对 php 比较陌生,需要一些帮助。

我有一个 html 表单,当用户可以提交文本时。此文本由 php 处理,如果有超过 20 个字符的行,则发生换行符。 之后,所有文本都“爆炸”了,我得到了一个数组。到目前为止一切都很好。

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'];
    $title= strtoupper($_POST['titre']);
    $txtcontent = $_POST['texte'];
    $txtcontent =  wordwrap($txtcontent,20,hexToStr('0D0A'),true);
    $txtcontent = explode("\n", $txtcontent);

print_r(array_chunk($txtcontent, 9, false));



$filecontent = implode(array_chunk($txtcontent, 9, false)); 

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);

我使用array_chunk 将数组$txtcontent 分隔在9 个元素的数组块中。

现在,我需要根据键值在每个新数组元素的开头和结尾添加一些常量文本。 也许我需要做一个循环或类似的事情,我已经尝试了很多次但仍然不能。 我需要帮助才能做到这一点。

示例: 如果我在网络表单中提交此文本:

line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12

我将拥有:

Array ( [0] => Array ( [0] => line1 [1] => line2 [2] => line3 [3] => line4 [4] => line5 [5] => line6 [6] => line7 [7] => line8 [8] => line9 ) [1] => Array ( [0] => line10 [1] => line11 [2] => line12 ) ) 

这是我需要帮助的地方:

处理后(添加常量文本后,根据键值),应该是这样的。但是我不知道我该怎么做。

Array ( [0] => Array ( 
[0] => \Text 1,1,"line1"
[1] => \Text 7,1,"line2"
[2] => \Text 13,1,"line3"
[3] => \Text 19,1,"line4"
[4] => \Text 25,1,"line5"
[5] => \Text 31,1,"line6"
[6] => \Text 37,1,"line7"
[7] => \Text 43,1,"line8"
[8] => \Text 49,1,"line9"
) [1] => Array ( 
[0] => \Text 1,1,"line10"
[1] => \Text 7,1,"line11"
[2] => \Text 13,1,"line12"
) ) 

最后,当我将它输出到文件时,我将拥有:

(我不知道如何输出array_chunk,我也需要帮助。)

\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"

注意数组中的元素个数可大可小,这只是一个例子。

注意另外,具有相同键的数组(例如:第 1 行和第 10 行)将在末尾和开头具有相同的额外文本。

我认为这个问题很难,但我希望有人能帮助我。我已经在一周前尝试解决它,但我仍然无法解决。

【问题讨论】:

    标签: php arrays forms output


    【解决方案1】:

    我希望这会有所帮助,但这个问题很重要:

    你可以在你用array_chunk创建的多维数组上做一个foreach循环,然后像这样添加你想要添加的文本:

    $chunkedarray = array_chunk($txtcontent, 9, false);
    foreach($chunkedarray as $key => $array){
        foreach($array as $k => $v){
            $chunkedarray[$key][$k] = '\text '.($k*6+1).',1,'.$v;
        }
    }
    

    然后你可以用我从@alienwebguy 在this stackoverflow question 上找到的一个函数来展平多维数组:

    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; 
    } 
    $flattenedarray = array_flatten($chunkedarray);
    

    然后你可以用另一个for循环输出扁平的多维数组:

    foreach($flattenedarray as $v){
      echo $v.'<br>';
    }
    

    查看 IDEone.com 上的示例代码:http://ideone.com/vzPJOB

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2017-02-13
      • 2017-03-28
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2018-11-09
      • 1970-01-01
      相关资源
      最近更新 更多