I am working on the application to get a response from the server in JSON format I need the response in this format
{
"data" :[
{
"cat_id" : "1",
"post_id" : "2",
"sticker_info":[
],
"text_info" : [
{
"font_family" : "arial.otf",
"text" : "MY NAME",
"text_id" : "1",
"txt_color" : "#000000",
"txt_height" : "7.6",
"txt_order" : "1",
"txt_rotation" : "0",
"txt_width" : "96.5",
"txt_x_pos" : "70.2",
"txt_y_pos" : "4.7"
},
what i tried so for
$sql = "SELECT text_id,my_text,text_colour,x_position,y_position,width,height,rotation,text_order,font_family,cat_id,post_id FROM text_info where cat_id='$cat_id'";
$result = $conn->query($sql);
//store the entire response
$response = array();
//the array that will hold the titles and links
$posts = array();
while($row=$result->fetch_assoc()) //mysql_fetch_array($sql)
{
$text_id = $row['text_id'];
$my_text = $row['my_text'];
$text_colour = $row['text_colour'];
$x_position= $row['x_position'];
$y_position= $row['y_position'];
$width = $row['width'];
$height = $row['height'];
$rotation = $row['rotation'];
$text_order = $row['text_order'];
$font_family = $row['font_family'];
$cat_id = $row['cat_id'];
$post_id = $row['post_id'];
$text_info[]=array('text_id'=> $text_id, 'my_text'=> $my_text,'text_colour'=> $text_colour);
$output = array('text_information' => $text_info);
//each item from the rows go in their respective vars and into the posts array
$posts[] = array('text_id'=> $text_id, 'my_text'=> $my_text,'text_colour'=> $text_colour, 'x_position'=> $x_position,'y_position'=> $y_position, 'width'=> $width,'height'=> $height, 'rotation'=> $rotation,'text_order'=> $text_order, 'font_family'=> $font_family,'cat_id'=> $cat_id, 'post_id'=> $post_id
);
array_push($posts,$output);
}
//the posts array goes into the response
$data1=json_encode($posts);
$response['error'] = false;
$response['message'] = 'Data Retrived successfull';
$response['data'] = $data1;
and the output from the above code is
[
{
"text_id":"1"
,"my_text":"25"
,"text_colour":"#682e2e"
,"x_position":"42.5"
,"y_position":"17.5"
,"width":"57.7"
,"height":"29.4"
,"rotation":"0"
,"text_order":"3"
,"font_family":"arial.ttf"
,"cat_id":"1"
,"post_id":"1"
},
{"text_information":[
{"text_id":"1"
,"my_text":"25"
,"text_colour":"#682e2e"
}
]
},
how to get the required result I tried a lot but can't find the solution I want the tex_information to be inside the array not in an object format that is shown in top my actual requirement.