Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中如何循环语句结果以插入关联数组_Php_Arrays_Loops_Associative Array - Fatal编程技术网

在PHP中如何循环语句结果以插入关联数组

在PHP中如何循环语句结果以插入关联数组,php,arrays,loops,associative-array,Php,Arrays,Loops,Associative Array,我似乎无法获得正确的语法来循环我的结果并将它们插入关联数组中——目前它只给出第一个结果,而没有循环其余的结果。我尝试了很多foreach等的组合,但无法成功完成 这是我的密码: $count = array( "timeinsert"=>"value", "rfid"=>"value" ); $readlags = "SELECT id_arduino, Devic

我似乎无法获得正确的语法来循环我的结果并将它们插入关联数组中——目前它只给出第一个结果,而没有循环其余的结果。我尝试了很多foreach等的组合,但无法成功完成

这是我的密码:

$count = array(
    "timeinsert"=>"value",
    "rfid"=>"value"
  );
  
  $readlags = "SELECT id_arduino, Device_id,time_inserted, message, message_type,
                    LAG(message) OVER(PARTITION BY device_id ORDER BY time_inserted) 
                    AS 'Previous Entry'
                    FROM Arduino_Data 
                    WHERE Datediff(Curdate(), time_inserted) < $period AND Device_id = $device AND message != 2
                    GROUP BY time_inserted ASC";
  
  $result = $conn->query($readlags);
  
  if (!$result) {
      echo $conn->error;
  }
  
  while ($row = mysqli_fetch_array($result)) {
      $eventtime = $row['time_inserted'];
      $message = $row['message'];
      $message_type = $row['message_type'];
      $previous = $row['Previous Entry'];
  
      if ($previous != 'Broken' and $previous != null) {
          $catid = "SELECT RFID_id
                    FROM Cat_User
                    WHERE RFID_id = $previous ";
  
          $result1 = $conn->query($catid);
  
          if (!$result1) {
              echo $conn->error;
          }
          while ($row1 = mysqli_fetch_array($result1)) {
              $cat_id = $row1['RFID_id'];
              //echo $cat_id . '<br>';
  
              }
          }
  
       
          if ($message == "Broken" && $message_type == "BreakBeam" && $previous==$cat_id) {
              
            
            $count["timeinsert"] = $eventtime;
              $count["rfid"]=$previous;

          }
            
      }       
  

 
  print_r($count);
当我执行数组_推送时,结果如下所示:

Array
(
    [0] => 2020-09-17 15:51:37
    [1] => 23641
    [2] => 2020-09-17 15:52:20
    [3] => 5609
    [4] => 2020-09-17 15:53:23
    [5] => 5609
    [6] => 2020-09-17 16:02:44
    [7] => 5609
)

这是因为您在同一个变量中覆盖了所有结果,所以当您打印reasult时,您将看到在变量中写入的最后一个数据

1)将$count定义为数组,将$c定义为这样的数组元素计数

$count=array();
$c=0;
2)当您想保存新数据,但需要将其保存在多维数组中时,如下所示:

$count[$c][“timeinsert”]=$eventtime;
$count[$c][“rfid”]=$previous;
$c++;
3)按如下方式检索数据:

$c=count($count);

对于($i=0;$iThanks,我想知道如何将两个结果分组到一个数组中?例如[0]=>array(timeinsert]=>2020-09-17 16:02:44[rfid]=>5609),因为现在的结果是这样的[0]=>array([timeinsert]=>2020-09-17 15:51:37)[1]=>array([rfid] => 23641 )
Array
(
    [0] => 2020-09-17 15:51:37
    [1] => 23641
    [2] => 2020-09-17 15:52:20
    [3] => 5609
    [4] => 2020-09-17 15:53:23
    [5] => 5609
    [6] => 2020-09-17 16:02:44
    [7] => 5609
)