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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵