Php json编码
我试图改变这些数据,这基本上是从一个查询中得到的。我想将每个部分的数据组合在一个数组中,然后将结果编码为jsonPhp json编码,php,json,Php,Json,我试图改变这些数据,这基本上是从一个查询中得到的。我想将每个部分的数据组合在一个数组中,然后将结果编码为json Array ( [sectionName] => Section C [di] => Robert 2013-10-02 [timeSpent] => 1970,0,1,00,00,06 ) Array ( [sectionName] => Section C [di] => John 2013-10-09 [timeSpent] => 1970
Array ( [sectionName] => Section C [di] => Robert 2013-10-02 [timeSpent] => 1970,0,1,00,00,06 )
Array ( [sectionName] => Section C [di] => John 2013-10-09 [timeSpent] => 1970,0,1,02,13,06 )
Array ( [sectionName] => Section D [di] => Tim 2013-10-03 [timeSpent] => 1970,0,1,00,00,17 )
进入
大体上
{
type:"column",
name: "SectionName",
color: "Highcharts.getOptions().colors[1]",
data: [{name:di,y:Date.UTC(timeSpent)},{di2 for same section},{di3 etc}]
我试过了
$i=0;
while($row = db_fetch_array($result)) {
//print_r($row);
$responce[$i][type]='column';
$responce[$i][name]=$row[sectionName];
$responce[$i][color]='Highcharts.getOptions().colors['.$i.']';
$responce[$i][data]=
//the problem comes here where i want to combine data depends on sections
//for each section **di and timeSpent**
'['. array(
name=>$row[di],
y=>'Date.UTC('.$row[timeSpent].')'
).']';
$i++;
}
return json_encode(responce); etc
这是一个很长的描述。我希望你能理解 假设您的数据按
sectionName
排序,那么您可以保留最后一行名称的引用,以检查是否需要创建新的响应行或只是将数据附加到当前响应行,以下是该方法的实现:
$i = -1;
$responce = array();
$lastSection = NULL;
while($row = db_fetch_array($result)) {
if($row['sectionName'] != $lastSection){
$lastSection = $row['sectionName'];
$i++;
$responce[$i]['type'] = 'column';
$responce[$i]['name'] = $row['sectionName'];
$responce[$i]['color']='Highcharts.getOptions().colors['.$i.']';
}
$responce[$i]['data'][]= (object)array('name'=>$row['di'],'y'=>'Date.UTC('.$row['timeSpent'].')');
}
return json_encode(responce);
我认为php中没有任何可用的JSON库可以帮助您。你必须按照你的进度去做
$i = -1;
$responce = array();
$lastSection = NULL;
while($row = db_fetch_array($result)) {
if($row['sectionName'] != $lastSection){
$lastSection = $row['sectionName'];
$i++;
$responce[$i]['type'] = 'column';
$responce[$i]['name'] = $row['sectionName'];
$responce[$i]['color']='Highcharts.getOptions().colors['.$i.']';
}
$responce[$i]['data'][]= (object)array('name'=>$row['di'],'y'=>'Date.UTC('.$row['timeSpent'].')');
}
return json_encode(responce);