dongxinyue2817 2014-12-19 17:16
浏览 67
已采纳

为while循环的每一行生成特定的数组

So I have a while loop which generates all the inbox messages for a user, then inside that while loop, I have a query and another while loop which generate all the recipients of that message. I am trying to build up an array from that inside while loop which will echo all the recipients/participants of each individual message thread. As is, when I var_dump my usernamesarray, It gets built up to contain all the recipients of all the messages (by the final time it is echoed). SO, how would I go about making this array unique to each row? I am sure this is a common issue, but I cannot seem to find the search terms to learn much about it.

 if (count($numbermessages)) {
        while ($stmt->fetch()) {
            $getrecipusernames= $mysqli->prepare("SELECT distinct(message_recips.userid), users.username, users.id FROM message_recips INNER JOIN users ON users.id=message_recips.userid WHERE messageid=?");
            $getrecipusernames->bind_param('i', $messageid);
            $getrecipusernames->execute();
            $getrecipusernames->store_result();
            $getrecipusernames->bind_result($recipuserid, $recipusername, $recipuserid1);
            $getrecipusernames->fetch();
                 while ($getrecipusernames->fetch()) {
                      $usernamesarray[] = $recipusername;
                  }

            $date_time=$created_on;
            $dte_new=date("l H:i A", strtotime($date_time));
               if ($status=="A"){
            $message_read_status="read";
               }
            else {
            $message_read_status="unread";
            }

Sincere thanks for any help!

  • 写回答

2条回答 默认 最新

  • duanbi9202 2014-12-19 18:28
    关注

    The solution was to declare the array prior to filling it, outside of the while loop:

     $usernamesarray = array();
     while($getrecipusernames->fetch()){
         $usernamesarray[] = $recipusername;
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染