我想显示每月的带宽使用情况,
有 2 个用户都在六月登录
这是我的代码:

<?php foreach ($data as $row):

            $date = $row['acctstarttime'];
            $d = date_parse_from_format("Y-m-d", $date);

             $monthNum  = $d["month"];
             $dateObj   = DateTime::createFromFormat('!m', $monthNum);
             $monthName = $dateObj->format('F');
                        ?>

                <td><?php echo $monthName;?></td>
                </tr>
<?php endforeach;?>

输出是: - 六月
- 六月
- 七月

我需要的输出:-六月-七月

我试图将我的查询更改为:
Function showData()
    {

    $this->db->distinct('radacct.acctstarttime');
    $this->db->from('radacct');
    $this->db->where('radacct.acctstarttime','y-06-d-h');
    $query = $this->db->get();


    If ($query->num_rows()>0)
    {
      Return $query->result();
    }
    Else
    {
     Return array();
    }
 }

但它什么也没改变(我的查询错了吗?)

我也试过:foreach (array_unique($data) as $row)
但它删除了七月
<?php print_r($row['acctstarttime'])?> 的输出
是: 2017-06-08 04:12:00 2017-06-08 04:12:00 2017-07-12 05:00:00

最佳答案

试试这个:

    <?php
    $month = [];
    foreach ($data as $row):
        if(!in_array($d["month"], $month)) {
            $month[] = $d['month'];
            $date = $row['acctstarttime'];
            $d = date_parse_from_format("Y-m-d", $date);

            $monthNum  = $d["month"];
            $dateObj   = DateTime::createFromFormat('!m', $monthNum);
            $monthName = $dateObj->format('F');
            ?>

            <td><?php echo $monthName;?></td>
            </tr>

            <?php
        }
     endforeach;
  ?>

更新:
    $month = array();
    foreach ($data as $row):
       $date = $row['acctstarttime'];
       $d = date_parse_from_format("Y-m-d", $date);
       $monthNum  = $d["month"];
       $dateObj   = DateTime::createFromFormat('!m', $monthNum);
       $monthName = $dateObj->format('F');
       if(!in_array($monthName, $month)) {
            $month[] = $monthName;
            echo $monthName;
       }
     endforeach;

关于php - 如何删除数组中的重复项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44476443/

10-17 00:16