【问题标题】:mysql php array to specific formatmysql php数组到特定格式
【发布时间】:2016-05-31 07:59:09
【问题描述】:

我有以下代码:

   <?php
//mysqli query here

$table = array();
$table['cols'] = array(

array('id' => '', 'label' => 'Date', 'type' => 'date'),
array('id' => '', 'label' => 'Amount ', 'type' => 'number'),
); 
$rows = array();
foreach($result as $row){
$temp = array();

$date1 = date_create_from_format('Y-m-d H:i:s', $row['Date']);
$date2 = date_format($date1, 'm-d-Y');

$temp[] = array('v' => (string)'new Date("'.$date2.'")');
$temp[] = array('v' => (float) $row['Amount']);
$rows[] = array('c' => $temp);
}

$result->free();
$table['rows'] = $rows;
$jsonTable = json_encode($table, true);
echo $jsonTable;
?>

这会将以下数据输出到谷歌折线图:

{"cols":[
{"label":"Reading Date","type":"date"},
{"label":"Reading ","type":"number"},
"rows":[
{"c":[{"v":"new Date(10\/04\/2015)"},{"v":0.4}]},
{"c":[{"v":"new Date(02\/18\/2016)"},{"v":0.6}]}]}

但是我需要它采用以下格式才能使图表正常工作:

{"cols":[
      {"label":"Reading Date","type":"date"},
      {"label":"Cl Reading(mg\/l) ","type":"number"}
    ],
    "rows":[
      {"c":[{"v":new Date("10/04/2015")},{"v":0.4}]},
      {"c":[{"v":new Date("02/18/2016")},{"v":0.6}]}
    ]} 

所以我真的只看一行需要更改但似乎无法将其转换为我需要的格式:

$temp[] = array('v' => (string)'new Date("'.$date2.'")');

谁能帮我解决这个问题?我知道它可能很简单,但似乎无法得到它..

【问题讨论】:

    标签: php mysql arrays json format


    【解决方案1】:

    更易读的东西会有所帮助

    <?php
    //mysqli query here
    
    $table = array(
        'cols' => array(
            array('id' => '', 'label' => 'Date', 'type' => 'date'),
            array('id' => '', 'label' => 'Amount ', 'type' => 'number'),
        ),
        'rows' => array(),
    );
    foreach($result as $row){
        $date1 = date_create_from_format('Y-m-d H:i:s', $row['Date']);
        $date2 = date_format($date1, 'Y, m, d');   // this
        $table['rows'][] = array(
            'c' => array(
                array('v' => '\'Date('.$date2.')\''),  // and this
                array('v' => (float) $row['Amount']),
            )
        );
    }
    
    $result->free();
    $jsonTable = json_encode($table, true);
    echo $jsonTable;
    ?>
    

    现在,需要改变什么?

    编辑:
    好的,我现在明白了...您不想生成 JSON,您认为您想生成 javascript.. 不要.. 只保留 JSON...
    (见What is the difference between JSON and Object Literal Notation?

    事后生成 Date 对象。您将拥有日期字符串.. 只需传递 Date 构造函数即可。

    编辑 2:
    Google Documentation - Dates and Times Using the Date String Representation
    您只需将 new Date(...) 括在引号中(

    编辑 3: 我更新了我的代码块以反映这个谷歌文档

    【讨论】:

      【解决方案2】:

      感谢 Brad Kent 为我指明了正确的方向:

      <?php
      
      //mysql query 
      
      
          $table = array(
          'cols' => array(
              array('id' => '', 'label' => 'Date', 'type' => 'date'),
              array('id' => '', 'label' => 'Amount ', 'type' => 'number'),
      
          ),
          'rows' => array(),
      );
      foreach($result as $row){
          $date1 = date_create_from_format('Y-m-d H:i:s', $row['Date']);
          $date2 = date_format($date1, 'Y, m, d'); 
          $table['rows'][] = array(
              'c' => array(
                  array('v' => 'Date('.$date2.')'), 
                  array('v' => (float) $row['Amount'])
      
              )
          );
      }
      
      $result->free();
      $jsonTable = json_encode($table, true);
      echo $jsonTable;
      
          ?>
      

      【讨论】:

        猜你喜欢
        • 2016-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多