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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符