【问题标题】:PHP Array formatting to change brackets to quotesPHP数组格式将括号更改为引号
【发布时间】:2014-01-10 16:42:15
【问题描述】:
<?php
$dataArray=array();  
//get data from database
$sql="SELECT MONTHNAME(date) as month, DAY(date) as day, YEAR(date) as year, date, AVG(score) as score FROM post_appt_survey WHERE date BETWEEN DATE_ADD(NOW(), INTERVAL -1 YEAR) AND NOW() GROUP BY MONTH(date) order by YEAR(date) asc, MONTH(date) asc";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
  while ($row = mysql_fetch_assoc($result)) {
      $day=$row["day"];
      $date=$row["date"];
      $month=$row["month"];
      $year=$row["year"];
      $score=$row["score"];
      //add to data array
      $dataArray[$month.' '.$year]=$score;

  }
}
print_r($dataArray, false);
?>

我的输出显示为

数组([2013 年 8 月] => 9.3333 [2013 年 9 月] => 10.0000 [2013 年 10 月] => 7.0000 [2013 年 11 月] => 8.5000 [2013 年 12 月] => 8.7500 [2014 年 1 月] => 6.3333) p>

我需要它读作

数组(“2013 年 8 月”=>9.3333“2013 年 9 月”=>10.0000“2013 年 10 月”=>7.0000“2013 年 11 月”=>8.5000“2013 年 12 月”=>8.7500“2014 年 1 月”=>6.3333) p>

请帮忙

【问题讨论】:

  • 这是标准 print_r 函数返回的内容。如果你想以不同的方式显示它,那么你需要手动遍历 $dataArray 数组并以你喜欢的格式显示它。
  • 尝试使用 foreach ($dataArray as $key => $val){echo $key ."=>" 。 $val; } 例如。

标签: php arrays formatting


【解决方案1】:

如果输出中没有其他方括号(看起来如此),请尝试:

$output = print_r($dataArray, true);
$output = str_replace(array('[', ']'), '"', $output);
echo $output;

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多