title: PHP array element need to output the same on my sample
body: <p>I have this code below that I want to make the output  like this json:  </p>
  
  <pre><code> [{avatarUrl: '',messages: 
         [{time: ,text: ''},
          {time: ,text: ''},
          {time: ,text: ''}]}
   ,{own: true,messages:
        [{time: ,text: ''},
         {time: ,text: ''},
         {time: ,text: ''}]}
   ,{avatarUrl: '',messages: 
         [{time: ,text: ''},
          {time: ,text: ''},
          {time: ,text: ''},
          {time: ,text: ''},
          {time: ,text: ''}]}
   ,{own: true,messages:
        [{time: ,text: ''},
         {time: ,text: ''},
         {time: ,text: ''}]}]
  </code></pre>
  
  <p>Because on my current codes the output is this, I also added the exact result of my mysql fetch array :</p>
  
  <pre><code>[{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:31:52","text":"hellooo"}]},
  {"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:31:54","text":"hi"}]},
  {"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:31:56","text":"yes"}]},
  {"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:31:58","text":"ano yun"}]},
  {"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:32:20","text":"jjj"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:32:22","text":"kasdkaskds"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:47","text":"yes"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:50","text":" WHatzz"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:51","text":"up"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:52","text":"jejes"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:53","text":"s"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 15:33:53","text":"ssSs"}],"own":true},{"avatarUrl":"assets\/img\/avatars\/avatar_02_tn.png","messages":[{"time":"2019-02-22 ...
  
  
  
  empfrom  empto   empmsg      xdatetime            xuser   
  -------  ------  ----------  -------------------  --------
  119      144     hellooo     2019-02-22 15:31:52  119     
  119      144     hi          2019-02-22 15:31:54  119     
  119      144     yes         2019-02-22 15:31:56  119     
  119      144     ano yun     2019-02-22 15:31:58  119     
  144      119     jjj         2019-02-22 15:32:20  144     
  144      119     kasdkaskds  2019-02-22 15:32:22  144     
  144      119     yes         2019-02-22 15:33:47  144     
  144      119      WHatzz     2019-02-22 15:33:50  144     
  144      119     up          2019-02-22 15:33:51  144     
  144      119     jejes       2019-02-22 15:33:52  144     
  144      119     s           2019-02-22 15:33:53  144     
  144      119     ssSs        2019-02-22 15:33:53  144     
  144      119     s           2019-02-22 15:33:53  144     
  144      119     s           2019-02-22 15:33:53  144     
  144      119     s           2019-02-22 15:33:53  144     
  144      119     s           2019-02-22 15:33:54  144     
  144      119     s           2019-02-22 15:33:54  144     
  144      119     sa          2019-02-22 15:33:54  144     
  144      119     a           2019-02-22 15:33:54  144     
  144      119     as          2019-02-22 15:33:54  144     
  144      119     d           2019-02-22 15:33:55  144     
  144      119     as          2019-02-22 15:33:56  144     
  144      119     jsjsd       2019-02-22 15:34:04  144     
  144      119     asd         2019-02-22 15:34:04  144     
  144      119     as          2019-02-22 15:34:04  144     
  144      119     a           2019-02-22 15:34:05  144     
  144      119     ajsjsjksd   2019-02-22 15:34:06  144     
  144      119     sdasd       2019-02-22 15:34:07  144     
  144      119     wew         2019-02-22 15:34:08  144   
  </code></pre>
  
  <p>And this is my code. I already add some trappings to copycat the exact format but I cant still replicate. Any idea or do I neeed to add some condition.</p>
  
  <pre><code>       $json_response_list = array();
          $json_response = array();
          $json_msgx = array();
          $own = 0;
          $from = 0;
          $mydata = "";
          $ctr = 1;
          $cx = 1;
          $sql = "SELECT empfrom,empto,empmsg,xdatetime,xuser FROM chatbox WHERE xdelete = 0  AND (empto = '".$_POST['userto']."' OR empfrom = '".$_POST['userto']."') AND (empto = '".$_SESSION['crmuserid']."' OR empfrom = '".$_SESSION['crmuserid']."' )  ORDER BY xdatetime ASC";
          $result = mysqli_query($conn1,$sql) or die(mysqli_error($sql));
          while ($row = mysqli_fetch_array($result)) {
              if($row['xuser']==$_SESSION['crmuserid']){
                  if($own==0){
                      $json_response['own'] = true;
                      $own = 1;
                      $from = 0;
                      $cx = 1;
                  }
              }else{
                  if($from==0){
                      $json_response['avatarUrl'] = "assets/img/avatars/avatar_02_tn.png";
                      $from = 1;
                      $own = 0;
                      $cx = 1;
                  }
              }
  
              $json_response['messages'] = array();
              $json_msgx['time'] = $row['xdatetime'];
              $json_msgx['text'] = $row['empmsg'];
              array_push($json_response['messages'],$json_msgx);  
              if($ctr!=1 &amp;&amp; $cx==1){
                  array_push($json_response_list,$json_response);     
              }
  
              $ctr++;
              $cx++;
          }
  
          $jsonData = json_encode($json_response_list); //JSON_PRETTY_PRINT
          echo $jsonData; 
  </code></pre>