title: Dynamically create PHP JSON array
body: <p>I have created a JSON in PHP as follows.</p>
  
  <pre><code>  $result=mysqli_query($mysqli,"SELECT * FROM service_provide WHERE personal_id='".$personal_id."'") or die(mysqli_error($mysqli);
      $row = mysqli_fetch_assoc($result);
  while($row = mysqli_fetch_assoc($result)) 
  {
  
  
  
    $data[] = array( 'regular'=&gt;array(
      ' fname'   =&gt; $row['fname'],   
        ' email_id'   =&gt; $row['email_id'], 
        ' phone_number'   =&gt; $row['phone_number'],
        ' state'   =&gt; $row['state'],
        ' city'   =&gt; $row['city'],
        ' main_id'   =&gt; $row['main_id'],
        ' sub_id'   =&gt; $row['sub_id'],
        ' service_id'   =&gt; $row['service_id'],
        'portfolio1'  =&gt; $row['portfolio1'],
        'portfolio2'  =&gt; $row['portfolio2'],
        'portfolio3'  =&gt; $row['portfolio3'])
    );
  
  
      }
        $json = json_encode($data);
        echo  $json;
  </code></pre>
  
  <p>The JSON is as follows</p>
  
  <pre><code> [{"regular":{" fname":"47788656"," email_id":"47788656"," phone_number":"47788656"," state":"47788656"," city":"47788656"," main_id":"47788656"," sub_id":"47788656"," service_id":"47788656","portfolio1":"47788656","portfolio2":"47788656","portfolio3":"47788656"}},
  
  {"regular":{" fname":"123656"," email_id":"123656"," phone_number":"123656"," state":"123656"," city":"123656"," main_id":"123656"," sub_id":"123656"," service_id":"123656","portfolio1":"123656","portfolio2":"123656","portfolio3":"123656"}},
  
  {"regular":{" fname":"9875656"," email_id":"9875656"," phone_number":"9875656"," state":"9875656"," city":"9875656"," main_id":"9875656"," sub_id":"9875656"," service_id":"9875656","portfolio1":"9875656","portfolio2":"9875656","portfolio3":"9875656"}},]
  </code></pre>
  
  <p>As you can see, the "Regular" tag inside the JSON is repeating. However, i wanted the JSON as follows, with just one "regular" array. </p>
  
  <pre><code>[{"regular":{" fname":"47788656"," email_id":"47788656"," phone_number":"47788656"," state":"47788656"," city":"47788656"," main_id":"47788656","sub_id":"47788656","service_id":"47788656","portfolio1":"47788656","portfolio2":"47788656","portfolio3":"47788656"},
  
  },
  {" fname":"123656"," email_id":"123656"," phone_number":"123656"," state":"123656"," city":"123656"," main_id":"123656"," sub_id":"123656"," service_id":"123656","portfolio1":"123656","portfolio2":"123656","portfolio3":"123656"},
  
  {" fname":"123656"," email_id":"123656"," phone_number":"123656"," state":"123656"," city":"123656"," main_id":"123656"," sub_id":"123656"," service_id":"123656","portfolio1":"123656","portfolio2":"123656","portfolio3":"123656"}}]
  </code></pre>
  
  <p>How can i tweak the PHP code to create a dynamic JSON as above?</p>