dtslobe4694 2019-03-01 17:33
浏览 517
已采纳

使用MySQL数据构建JSON多维数组

I'm trying to build a multidimensional array in JSON using data from MySQL. I need my JSON to looks like this:

[
    {
        "name": "test name",
        "dates": {
            "Feb 26 2019": 2,
            "Feb 27 2019": 5,
            "Feb 28 2019": 8
        }
    },
        {
        "name": "test name 2",
        "dates": {
            "Feb 22 2019": 2,
            "Feb 24 2019": 5,
            "Feb 28 2019": 8
        }
    }
]

The dates are how many times the name appears in the MySQL table. For example, "test name" shows up 2 times with the date of "Feb 26 2019".

Here is my php:

$query = $wpdb->get_results( "SELECT date, count(date) AS `count` FROM $table_name group by date" );
foreach ($query as $row) {
    $valid_date = date( 'M d Y', strtotime($row->date));
    $data[] = array('date' => $valid_date, 'count' => $row->count);
}


$query1 = $wpdb->get_results( "SELECT DISTINCT cuname FROM $table_name ORDER BY cuname" );
foreach ($query1 as $row) {
    $names[] = array('name' => $row->cuname, 'date' => $data);
}

print json_encode($names);

Here is the JSON that is getting returned:

[  
    {  
        "name":"test name",
        "date":[  
            {  
                "date":"Feb 22 2019",
                "count":"9"
            },
            {  
                "date":"Feb 23 2019",
                "count":"14"
            },
            {  
                "date":"Feb 24 2019",
                "count":"9"
            },
            {  
                "date":"Feb 25 2019",
                "count":"7"
            },
            {  
                "date":"Feb 26 2019",
                "count":"1"
            }
        ]
    },
    {  
        "name":"test name 2",
        "date":[  
            {  
                "date":"Feb 22 2019",
                "count":"9"
            },
            {  
                "date":"Feb 23 2019",
                "count":"14"
            },
            {  
                "date":"Feb 24 2019",
                "count":"9"
            },
            {  
                "date":"Feb 25 2019",
                "count":"7"
            },
            {  
                "date":"Feb 26 2019",
                "count":"1"
            }
        ]
    }
]

The issue is that all dates and the number of times the dates appear in the table are showing up for each "name". I only need the dates for how many times each name appears.

  • 写回答

2条回答 默认 最新

  • dongmu1951 2019-03-01 18:47
    关注

    I'd do it with a single query:

    $sql = "
    SELECT 
        `cuname` AS `name`, 
        `date`,
         count(`date`) as `datecount`
    FROM 
        $table_name
    GROUP BY 
        `name`, `date`
    ";
    
    
    $query = $wpdb->get_results($sql);
    
    $res = array();
    $i = 0;
    $name = null;
    foreach ($query as $row) {
        if ($name!=$row->name && $name != null) {
            $i++;
        }
        $res[$i][$row->name] = $row->name;
        $res[$i]['dates'][$row->date] = $row->datecount;
    
        $name = $row->name;
    }
    
    //Verify the array structure:
    
    echo "<pre>";
    print_r($res);
    echo "</pre>";
    
    //Build JSON:
    $json = json_encode($res);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dsgo31121 2019-03-01 17:48
    关注

    Try to set your dates and count as key: value

    foreach ($query as $row) {
        $valid_date = date( 'M d Y', strtotime($row->date));
        $data[$valid_date] = $row->count;
    }
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我不明白为什么c#微软的官方api浏览器为什么不支持函数说明的检索,有支持检索函数说明的工具吗?
  • ¥15 ORBSLAM2框架跑ICL-NUIM数据集
  • ¥15 在我想检测ros是否成功安装时输入roscore出现以下
  • ¥30 老板让我做一个公司的投屏,实时显示日期,时间,安全生产的持续天数,完全没头绪啊
  • ¥15 Google Chrome 所有页面崩溃,三种解决方案都没有解决,我崩溃了
  • ¥20 使用uni-app发起网络请求,获取重定向302返回的cookie
  • ¥20 手机外部浏览器拉起微信小程序支付 (相关搜索:微信小程序)
  • ¥20 怎样通过一个网址找到其他同样模版的网址
  • ¥30 XIAO esp32c3 读取FDC2214的数据
  • ¥15 在工控机(Ubuntu系统)上外接USB蓝牙硬件进行蓝牙通信