PHP数组转换为带换行符的字符串

时间:2018-06-25 07:37:52

标签: php arrays loops

我想将数组转换为带有换行符的字符串,以实现最简单的文本格式。该数组是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
}

1 个答案:

答案 0 :(得分:0)

这是答案。 “谢谢”所有的反对。

  $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...