Aiswarya Krishna Aiswarya Krishna - 5 months ago 6
JSON Question

Including an array inside PHP JSON

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(
' 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;


Now I have another array in PHP that I want to include to the above JSON. The array is as follows.

$service_title=array();
$result5=mysqli_query($mysqli,"SELECT * FROM request_submission WHERE req_personal_id='".$req."'") or die(mysqli_error($mysqli);

while($row5 = mysqli_fetch_assoc($result5))

{
$service_title[]=$row5["service_title"];


}


How do in insert the above array into the PHP JSON I mentioned above?

Answer

You have two array. 1. $data and 2. $service_title.

so you can use array_merge() php function and convert new array in to json.

something like this.

$result = array_merge($data, $service_title);
$json = json_encode($result);
echo  $json;
Comments