【问题标题】:PHP array to string with newline charactersPHP数组到带有换行符的字符串
【发布时间】:2018-06-25 07:37:51
【问题描述】:

我想将数组转换为带有换行符的字符串,以实现最简单的文本格式。该数组是 mysql 选择查询的结果,我想我需要一个循环,它使用 implode 函数转换为字符串并用某些东西(即“ * ”)分隔字段,并在每行的末尾添加一个换行符。

样本输出

  2018-06-22 * meeting * They didn't want to buy anythin
  2018-06-23 * * called and wanted to buy something
  2018-06-24 * meeting * Gave specification

我在想这样的事情(但我错了,这就是我问的原因):

      $Diary =''; // start with empty string
      $array = mysqli_fetch_array($fetch);// fetched already
      $length = count($array);

      for ($x = 0; $length; $x++ + 3) {
      $temparray = // use a temp array for one row only
      $Diary = // increment a string until the end of the row, add newline at the end
}

【问题讨论】:

  • 你能贴出你试图解决这个问题的代码吗?
  • 你能显示示例输出吗?
  • 欢迎来到 Stack Overflow。您有示例输入吗?到目前为止,您尝试了哪些代码?
  • convert an array to string with newline characters :一个例子? result of a mysql select query :请向我们展示查询+结果。 I suppose I need a loop which uses the implode function (...):告诉我们你尝试了什么?顺便说一句,什么不起作用,我没有看到错误?

标签: php arrays loops


【解决方案1】:

这就是答案。 “谢谢”大家的反对。

  $Diary ='';   
  $query  = 'SELECT tb2_date, tb2_note, tb2_entry from DiaryTable WHERE tb2_tb1ID = 
  "'.$HiddenID.'" ORDER BY tb2_date DESC';
  $resulto = mysqli_query($dbc, $query);

while ($rows = mysqli_fetch_row($resulto)) {
    $rowimploded = implode(' ** ', $rows);  
    $newline = "\r\n";
    $Diary = $Diary.$rowimploded.$newline;      
}
mysqli_free_result($resulto);

// echo $Diary or whatever...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-09
    • 2021-07-03
    • 2017-05-01
    • 1970-01-01
    • 2012-03-25
    • 2013-06-30
    • 2010-09-19
    • 1970-01-01
    相关资源
    最近更新 更多