I have created a JSON in PHP as follows.
$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'=>array(
' fname' => $row['fname'],
' email_id' => $row['email_id'],
' phone_number' => $row['phone_number'],
' state' => $row['state'],
' city' => $row['city'],
' main_id' => $row['main_id'],
' sub_id' => $row['sub_id'],
' service_id' => $row['service_id'],
'portfolio1' => $row['portfolio1'],
'portfolio2' => $row['portfolio2'],
'portfolio3' => $row['portfolio3'])
);
}
$json = json_encode($data);
echo $json;
The JSON is as follows
[{"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"}},]
As you can see, the "Regular" tag inside the JSON is repeating. However, i wanted the JSON as follows, with just one "regular" array.
[{"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"}}]
How can i tweak the PHP code to create a dynamic JSON as above?
jsonis not valid. Please validate it and edit your question so we know what format you actually want. Also, where does theregularstring coming from?