首页 > 解决方案 > 在运行时为 php 对象分配名称

问题描述

我想根据所选月份选择员工的出勤详细信息。我想使用与月份相同的对象名称存储每个月的数据。我不明白如何在运行时分配名称。这是我在下面给出的代码

 for ($i=0; $i < $totalmonth; $i++) { 


  $query="SELECT attendance FROM attendance
    WHERE month='$m' && year='$y' && dept='$dept' && location='$loc'";

    $result = mysqli_query($connect, $query);
    if(mysqli_num_rows($result) > 0)
      {
          $dbdata = array();
          while ( $row = $result->fetch_assoc())
          {
               array_push($dbdata, $row);
          }
       }
    else
      {
         echo 'Data Not Found';
      }
         echo json_encode(array('data' => $dbdata));

    }

标签: php

解决方案


使用关联数组将月份名称指定为代码中的键,如下所示;

while ( $row = $result->fetch_assoc())
{
     $dbdata[$m]=$row;  
}

推荐阅读