我有一个通用的SQL查询构建器,它为我提供了包含以下列的统一表:
Name, Name2, Amount
查询以GROUP BY Name2, Name
结尾。
现在我想让它适合使用以下格式塔将其转换为JSON:
{dim2: Name2, data: [{label: Name, y: Amount},{label: Name, y: Amount},{label: Name, y: Amount},...]},
{dim2: Name2, data: [{label: Name, y: Amount},{label: Name, y: Amount},{label: Name, y: Amount},...]},
{dim2: Name2, data: [{label: Name, y: Amount},{label: Name, y: Amount},{label: Name, y: Amount},...]},
{dim2: Name2, data: [{label: Name, y: Amount},{label: Name, y: Amount},{label: Name, y: Amount},...]},
...
到目前为止,我做到了这一点:
while($result = mysqli_fetch_array($results))
{
if(!isset($return[$result['Name2']]))
$return[$result['Name2']] = array();
$return[$result['Name2']][] =
array(
'Label' => $result['Name'],
'y' => $result['Anzahl']
);
}
$filestring = json_encode($return, JSON_NUMERIC_CHECK);
哪个不起作用。整天工作真的很烦人,我现在非常感谢一些帮助,提前谢谢!!!
更新
这是一个如何计算到目前为止的例子:
[{"dim2":"Consumer Entertainment","data":{"Label":"Entertainment","y":1}},
{"dim2":"Digital Life","data":{"Label":"Electronic Devices","y":1}},
{"dim2":"Digital Life","data":{"Label":"Entertainment","y":1}},
{"dim2":"Digital Life","data":{"Label":"Financial Services","y":2}},
{"dim2":"Digital Life","data":{"Label":"IT","y":2}},
{"dim2":"Digital Life","data":{"Label":"Media & Publishing","y":1}},
{"dim2":"Digital Life","data":{"Label":"Transportation & Mobility","y":1}},
{"dim2":"Healthy Life","data":{"Label":"Fashion & Accessories","y":1}},
...]
我想:
[{"dim2":"Consumer Entertainment","data":[{"Label":"Entertainment","y":1}]},
{"dim2":"Digital Life","data":[{"Label":"Electronic Devices","y":1},
{"Label":"Entertainment","y":1},
{"Label":"Financial Services","y":2},
{"Label":"IT","y":2},
{"Label":"Media & Publishing","y":1},
{"Label":"Transportation & Mobility","y":1}]},
{"dim2":"Healthy Life","data":[{"Label":"Fashion & Accessories","y":1}]},
...]
等等
答案 0 :(得分:0)
现在它更容易理解:)
$ar = array('Label'=>'Entertainment', 'y'=>1);
$q[]=array('dim2'=>'Consumer Entertainment', 'data'=>array($ar));
$ar = array('Label'=>'Electrodivices', 'y'=>1);
$q[]=array('dim2'=>'Digital Life', 'data'=>array($ar));
$ar = array('Label'=>'Entertainment', 'y'=>2);
$q[]=array('dim2'=>'Digital Life', 'data'=>array($ar));
echo json_encode($q);
它给出了
[{“dim2”:“Consumer Entertainment”,“data”:[{“Label”:“Entertainment”,“y”:1}]},
{“dim2”:“Digital Life”,“data”:[{“Label”:“Electrodivices”,“y”:1}]},
{“dim2”:“数字生活”,“数据”:[{“标签”:“娱乐”,“y”:2}]}]
所以,你应该尝试写一些像
这样的东西$return[$result['Name2']][] =
array(array(
'Label' => $result['Name'],
'y' => $result['Anzahl']
));
它看起来多么愚蠢。可能它不明白它正在采取阵列? :P