简体   繁体   English

如何从 mysql 中获取价值在 php 中按月重新排列

[英]how to get value from mysql month wise rearrange in php

I want to get data from api and mysql also.我还想从 api 和 mysql 获取数据。 But my data is look like:但我的数据看起来像:

1 - 2019 - 7455
2 - 2019 - 5479
3 - 2019 - 11152
4 - 2019 - 12666
5 - 2019 - 16657
6 - 2019 - 10154
7 - 2019 - 8354
8 - 2019 - 10726
9 - 2019 - 10328
10 - 2019 - 11093
11 - 2019 - 9646
12 - 2019 - 9615

that 1 means January and 12 means December So I want rearrage the data to a table Like: 1表示 1 月, 12表示 12 月所以我想将数据重新排列到表格中,例如:

2019: 2019:

 table, table thead tr th, table tbody tr td{ border: 1px solid #000; }
 <h1>Year: 2019</h1> <table> <thead> <tr> <th scope="col">Jan</th> <th scope="col">Feb</th> <th scope="col">Mar</th> <th scope="col">Apr</th> <th scope="col">May</th> <th scope="col">Jun</th> <th scope="col">Jul</th> <th scope="col">Aug</th> <th scope="col">Sep</th> <th scope="col">Oct</th> <th scope="col">Nov</th> <th scope="col">Dec</th> </tr> </thead> <tbody> <tr> <td>7455</td> <td>5479</td> <td>11152</td> <td>12666</td> <td>16657</td> <td>10154</td> <td>8354</td> <td>10726</td> <td>10328</td> <td>11093</td> <td>9646</td> <td>9615</td> </tr> </tbody> </table>

But It shows like:但它显示如下:

在此处输入图像描述

I set values using from api:我使用 api 设置值:

$year = $field['date']['year'];
$month = $field['date']['month'];
$views = $field['sites']['views'];

So that it print the year every times when it is on foreach loops:这样它每次在 foreach 循环时都会打印年份:

But my month data is look like:但我的月份数据是这样的:

<?php for($mn=1;$mn <= 12;$mn++){?>
  <td>
   <?php 
      if($field['date']['month'] == $mn){
        echo $views;
      }
      else{
        echo ' ';
      }
   ?>
   </td>
   <?php } ?>

So How to re arrange the datas look like above table snippet?那么如何重新排列数据看起来像上面的表格片段?

One option is to pre-process your numbers into an easy to iterate array.一种选择是将您的数字预处理为易于迭代的数组。 Here's a solution that puts all the values in and fills in a zero for any missing values.这是一个将所有值放入并为任何缺失值填充零的解决方案。

<?php
// example code

$data = "1 - 2019 - 7455
2 - 2019 - 5479
3 - 2019 - 11152
4 - 2019 - 12666
5 - 2019 - 16657
6 - 2019 - 10154
7 - 2019 - 8354
8 - 2019 - 10726
10 - 2019 - 11093
11 - 2019 - 9646
12 - 2019 - 9615";
$data = explode("\n", $data);
$newdata = array();
foreach($data as $month) {
    $parts = explode("-", $month);
    if (!isset($newdata[trim($parts[1])])) {
        $newdata[trim($parts[1])] = array_fill(0,12,0);
    }
    $index = intval($parts[0])-1;
    $newdata[trim($parts[1])][$index] = intval($parts[2]);
}

print_r($newdata);

Output: Output:

Array
(
    [2019] => Array
        (
            [0] => 7455
            [1] => 5479
            [2] => 11152
            [3] => 12666
            [4] => 16657
            [5] => 10154
            [6] => 8354
            [7] => 10726
            [8] => 0
            [9] => 11093
            [10] => 9646
            [11] => 9615
        )

)

https://tehplayground.com/NvcEkqf0hMEyhzzO https://tehplayground.com/NvcEkqf0hMEyhzzO

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

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