如何json_encode'php array'

时间:2014-11-21 02:06:28

标签: php json

我有以下数组:

{"label":"label1","data":[[10,55],[15,32],[16,49]]}
{"label":"label","data":[[10,55],[15,32],[16,49]]} 

如何获取字符串字符(,)beetween {“label”:“label1”,“data”:[[10,55],[15,32],[16,49]]}和{“label” : “LABEL2”, “数据”:[[10,55],[15,32],[16,49]]} 像这样的结果..

{"label":"label1","data":[[10,55],[15,32],[16,49]]},
{"label":"label","data":[[10,55],[15,32],[16,49]]} 

代码

while($row = mysql_fetch_assoc($result))
{   
    $int = $row['SC'];
    $join = intval($int);
    $int2 = $row['jam'];
    $join2 = intval($int2);
    $dataset1[] = array($join2,$join);

}
for ($i=0; $i <2 ; $i++) { 
$dataset = array(label => label1, data => $dataset1);
$final = json_encode($dataset);

echo $final;

1 个答案:

答案 0 :(得分:2)

您需要将所有内容放入另一个数组中:

$dataset = array();
foreach ($dataset1 as $d) {
    $dataset[] = array('label' => $label, 'data' => $d);
}
$final = json_encode($dataset);
echo $final;

这应输出:

[{"label":"label1","data":[[10,55],[15,32],[16,49]]},
 {"label":"label","data":[[10,55],[15,32],[16,49]]}]