简体   繁体   English

如何通过匹配日期将mysqli_fetch_array结果分组?

[英]How do I group mysqli_fetch_array results by matching dates?

I am attempting to build a graph in morrisjs using php/mysql and need to output an object with the following format. 我正在尝试使用php / mysql在morrisjs中构建图形,并且需要输出以下格式的对象。 It should group the period dates and then list the names following it for matching results: 它应该对期间日期进行分组,然后列出其后的名称以匹配结果:

{period: "2019-02-06 12:00:00", shelly: 2147483647, debra: 1240996571, sally: 2147483647, bill: 2147483647, bob: 619685085, jim: 126614618},
{period: "2019-02-06 12:30:00", shelly: 2147483647, debra: 1240996571, sally: 2147483647, bill: 2147483647, bob: 619685085, jim: 126614618},
{period: "2019-02-06 13:00:00", shelly: 2147483647, debra: 1240996571, sally: 2147483647, bill: 2147483647, bob: 619685085, jim: 126614618}

PHP: PHP:

    while ($row = mysqli_fetch_array($poolChart)) {
        $chart_data .= "{ \"period\": \"".$row["StatsHistoryTime"]."\", \"name\": \"".$row["name"]."\", \"hashrate\":".$row["hashrate"]."}, ";

    }
    $chart_data = substr($chart_data, 0, -2);

Currently my output looks like this 目前我的输出看起来像这样

[
  {
    "period": "2019-02-06 12:00:00",
    "name": "shelly",
    "hashrate": 2147483647
  },
  {
    "period": "2019-02-06 12:00:00",
    "name": "debra",
    "hashrate": 1240996571
  },
  {
    "period": "2019-02-06 12:00:00",
    "name": "sally",
    "hashrate": 2147483647
  },
  {
    "period": "2019-02-06 12:00:00",
    "name": "bill",
    "hashrate": 2147483647
  }
  {
    "period": "2019-02-06 12:30:00",
    "name": "shelly",
    "hashrate": 2147483647
  },
  {
    "period": "2019-02-06 12:30:00",
    "name": "debra",
    "hashrate": 1460613388
  },
  {
    "period": "2019-02-06 12:30:00",
    "name": "sally",
    "hashrate": 2147483647
  },
  {
    "period": "2019-02-06 12:30:00",
    "name": "bill",
    "hashrate": 2147483647
  }
]

The way to solve your problem is to get your data into an appropriate format (an array for each value of period, with other values in the array being the combination of each name and hashrate ), and then you can use json_encode to convert that to the format required: 解决问题的方法是将数据转换为适当的格式(每个period值的数组,该数组中的其他值是每个namehashrate的组合),然后可以使用json_encode将其转换为所需格式:

$chartdata = array();
while ($row = mysqli_fetch_array($poolChart)) {
    $period = $row['StatsHistoryTime'];
    // have we already got data for this period?
    if (($k = array_search($period, array_column($chartdata, 'period'))) !== false) {
        // yes, update data for this period
        $chartdata[$k][$row['name']] = $row['hashrate'];
    }
    else {
        // no, create a new array
        $chartdata[] = array('period' => $period, $row['name'] => $row['hashrate']);
    }
}
echo json_encode($chartdata);

Output (for your sample data) 输出(用于样本数据)

[
  {
    "period":"2019-02-06 12:00:00",
    "shelly":2147483647,
    "debra":1240996571,
    "sally":2147483647,
    "bill":2147483647
  },
  {
    "period":"2019-02-06 12:30:00",
    "shelly":2147483647,
    "debra":1460613388,
    "sally":2147483647,
    "bill":2147483647
  }
]

Demo on 3v4l.org 3v4l.org上的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM