douzhan1935 2012-07-31 18:59
浏览 240

将几个MySQL结果合并为一个JSON编码(PHP)

Here is code as exists now:

while($row=mysql_fetch_assoc($count_query_result))
    $output[]=$row;
while($row=mysql_fetch_assoc($average_query_result))
    $output2[]=$row;
while($row=mysql_fetch_assoc($items_query_result))
    $output3[]=$row;

print(json_encode(array($output,$output2,$output3)));
mysql_close();

My question:

How do I take a single column from each of the three query results, and make a JSON array out of it, like so:

[{ 'att1' : 'data'}, { 'att2' : 'data'}, { 'att3' : 'data'}]

ASSUMING:

  • att1 came from the $count_query_result/$output
  • att2 came from the $average_query_result/$output2
  • att3 came from the $items_query_result/$output3

Therefore, encoding only one variable, not 3.

  • 写回答

2条回答 默认 最新

  • duanjue2560 2012-07-31 19:23
    关注
    $result = array('att1' => $row['data'],
                    'att2' => $row['data']
    
    echo json_encode($result)
    

    where $row['data'] is the information that you want returned from each of your queries

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口