title: How to group ID in while loop (for php json_encode)
body: <p>I have a problem for several days to create a Json array with MySQL data</p>
  
  <p>Here are my MySQL data:</p>
  
  <pre><code>zi  zn         vn                       pioo     pio     ve    station    es       eo  
  
   1  Zone 1     Value 01                  1     199       0        1       1         0
   1  Zone 1     Value 02                 54     637       0        3       0         0
   1  Zone 1     Value 03                 55     637       1        3       0         0
   2  Zone 2     Value 01                  1     199       0        1       1         0
   2  Zone 2     Value 03                 55     637       1        3       0         0
   2  Zone 2     Value 04                 56     642       0        3       0         0
   3  Zone 3     Value 01                  1     199       0        1       1         0
   3  Zone 3     Value 05                 57     647       1        3       0         0
   4  Zone 4     Value 05                 57     647       1        3       0         0
   5  Zone Test  Value 02                 54     637       0        3       0         0
   5  Zone Test  Value 03                 55     637       1        3       0         0
  </code></pre>
  
  <p>Here is my PHP code:</p>
  
  <p>My query is $query and my SQL connexion is $con</p>
  
  <pre><code>$result = array();
  $json_response = array();
  $response = array();
  
  
  if($stmt = $con-&gt;prepare($query)){
      $stmt-&gt;execute();
  
      $stmt-&gt;bind_result($zoneId,$zoneName,$vanneName,$piooId,$pioId,$vanne_etat,$station,$etat_station,$etat_ordre);
  
      while($stmt-&gt;fetch()){
  
  
              $json_response = [
                      'zi' =&gt; $zoneId,
                  'zn' =&gt; $zoneName,
                  $vanneName =&gt; [],
              ];
  
          $json_response[$vanneName][] = [
              'pioo' =&gt; $piooId,
              'pio' =&gt; $pioId,
              've' =&gt; $vanne_etat,
              'es' =&gt; $etat_station,
              'eo' =&gt; $etat_ordre,
          ];
  
  
  
      $data = [];
      foreach ($json_response as $element) {
          $data[] = $element;
      }
  
          $result[]=$json_response;
  
  
      }
      $stmt-&gt;close();
  
  
      $online = mysqli_fetch_array(mysqli_query($con, "SELECT CASE WHEN TIMESTAMP &gt; DATE_SUB(NOW(), INTERVAL 5 MINUTE) THEN 1 ELSE 0 END AS online FROM relevés_instantanés ORDER BY ID DESC LIMIT 1")) ['online'];
      $response["online"] = $online;
      $response["success"] = 1;
      $response["data"] = $result;
  
  
  }else{
      //Errors
      $response["success"] = 0;
      $response["message"] = mysqli_error($con);
  
  
  }
  
  mysqli_close($con);
  echo json_encode($response);
  </code></pre>
  
  <p>Output Json like that : </p>
  
  <pre><code>{
     "online":"1",
     "success":1,
     "data":[
        {
           "zi":1,
           "zn":"Zone 1",
           "Value 01":[
              {
                 "pioo":1,
                 "pio":199,
                 "ve":0,
                 "es":1,
                 "eo":0
              }
           ]
        },
        {
           "zi":1,
           "zn":"Zone 1",
           "Value 02":[
              {
                 "pioo":54,
                 "pio":637,
                 "ve":0,
                 "es":0,
                 "eo":0
              }
           ]
        },
        {
           "zi":1,
           "zn":"Zone 1",
           "Value 03":[
              {
                 "pioo":55,
                 "pio":637,
                 "ve":1,
                 "es":0,
                 "eo":0
              }
           ]
        },
        {
           "zi":2,
           "zn":"Zone 2",
           "Value 01":[
              {
                 "pioo":1,
                 "pio":199,
                 "ve":0,
                 "es":1,
                 "eo":0
              }
           ]
        },
        {
           "zi":2,
           "zn":"Zone 2",
           "Value 03":[
              {
                 "pioo":55,
                 "pio":637,
                 "ve":1,
                 "es":0,
                 "eo":0
              }
           ]
        },
        {
           "zi":2,
           "zn":"Zone 2",
           "Value 04":[
              {
                 "pioo":56,
                 "pio":642,
                 "ve":0,
                 "es":0,
                 "eo":0
              }
           ]
        },
        {
           "zi":3,
           "zn":"Zone 3",
           "Value 01":[
              {
                 "pioo":1,
                 "pio":199,
                 "ve":0,
                 "es":1,
                 "eo":0
              }
           ]
        },
        {
           "zi":3,
           "zn":"Zone 3",
           "Value 05":[
              {
                 "pioo":57,
                 "pio":647,
                 "ve":1,
                 "es":0,
                 "eo":0
              }
           ]
        },
        {
           "zi":4,
           "zn":"Zone 4",
           "Value 05":[
              {
                 "pioo":57,
                 "pio":642,
                 "ve":1,
                 "es":0,
                 "eo":0
              }
           ]
        },
        {
           "zi":5,
           "zn":"Zone Test",
           "Value 02":[
              {
                 "pioo":54,
                 "pio":637,
                 "ve":0,
                 "es":0,
                 "eo":0
              }
           ]
        },
        {
           "zi":5,
           "zn":"Zone Test",
           "Value 03":[
              {
                 "pioo":55,
                 "pio":637,
                 "ve":1,
                 "es":0,
                 "eo":0
              }
           ]
        }
     ]
  }
  </code></pre>
  
  <p>But I expect an ouput like that : (I am not sure of the syntax, but it is for you to understand my wish.)</p>
  
  <pre><code>{
     "online":"1",
     "success":1,
     "data":[
        {
  
           "zi":1,
           "zn":"Zone 1"{
           "Value 01":[
                  {
                     "pioo":1,
                     "pio":199,
                     "ve":0,
                     "es":1,
                     "eo":0
                  }
               ]
               "Value 02":[
                  {
                     "pioo":54,
                     "pio":637,
                     "ve":0,
                     "es":0,
                     "eo":0
                  }
               ]
               "Value 03":[
                  {
                     "pioo":55,
                     "pio":637,
                     "ve":1,
                     "es":0,
                     "eo":0
                  }
               ]
            },
  
  
        {
           "zi":2,
           "zn":"Zone 2"{
           "Value 01":[
              {
                 "pioo":1,
                 "pio":199,
                 "ve":0,
                 "es":1,
                 "eo":0
              }
           ]
           "Value 03":[
              {
                 "pioo":55,
                 "pio":637,
                 "ve":1,
                 "es":0,
                 "eo":0
              }
           ]
  
           "Value 04":[
              {
                 "pioo":56,
                 "pio":642,
                 "ve":0,
                 "es":0,
                 "eo":0
              }
           ]
        },
  
        {
           "zi":3,
           "zn":"Zone 3"{
           "Value 01":[
              {
                 "pioo":1,
                 "pio":199,
                 "ve":0,
                 "es":1,
                 "eo":0
              }
           ]
           "Value 05":[
              {
                 "pioo":57,
                 "pio":647,
                 "ve":1,
                 "es":0,
                 "eo":0
              }
           ]
        },
  
  
        {
           "zi":4,
           "zn":"Zone 4"{
           "Value 05":[
              {
                 "pioo":57,
                 "pio":642,
                 "ve":1,
                 "es":0,
                 "eo":0
              }
           ]
        },
  
  
        {
           "zi":5,
           "zn":"Zone Test"{
           "Value 02":[
              {
                 "pioo":54,
                 "pio":637,
                 "ve":0,
                 "es":0,
                 "eo":0
              }
           ]
           "Value 03":[
              {
                 "pioo":55,
                 "pio":637,
                 "ve":1,
                 "es":0,
                 "eo":0
              }
           ]
        }
     ]
  }
  </code></pre>
  
  <p>I can not isolate each id of Zones to correctly build my JSON ....</p>
  
  <p>Is it possible to use each ID once in the while loop?</p>
  
  <p>any help appreciated</p>
  
  <p>Sorry for my bad english .. :S</p>
  
  <p>Joel</p>