带有json嵌套数组的php

时间:2018-07-15 07:34:29

标签: php arrays json

我正在研究应用程序,以从服务器获取JSON格式的响应 我需要这种格式的回复

{
  "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"

    },

我为此做了什么

$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"
 }
 ]
 },

如何获得所需的结果,我尝试了很多,但是找不到解决方案 我希望tex_information不在数组中,而不是对象格式 在我的实际需求上方显示。


2 个答案:

答案 0 :(得分:2)

这将解决您的问题。在您的while loop中添加以下数组。

$text_info[]= ['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];


//each item from the rows go in their respective vars and into the posts array
$posts['data'][] = [
    'cat_id' =>$cat_id,
    "post_id" => "2",
    "sticker_info"=>[], 
    'text_info' => $text_info

]; 
        print_R(json_encode($posts));

输出:

    {"data":[{
       "cat_id":"1",
        "post_id":"2",
        "sticker_info":[],
        "text_info":[{
           "text_id":1,
            "my_text":"MY NAME", 
            "text_colour":"Yello",
            "x_position":"70.2", 
            "y_position":"4.7", 
            "width":"96.5", 
            "height":"7.6", 
            "rotation":"0", 
            "text_order":"1", 
            "font_family":"arial.otf"
        }]
     }]
}

答案 1 :(得分:0)

尝试此代码

$output = array();

while($row=$result->fetch_assoc()) //mysql_fetch_array($sql)
{ 

    $posts=array();

    $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'];


    $sticker_info[]=array('key'=>'value','key1'=>'value','key2'=>'value');

   $text_info[]=array('text_id'=> $text_id,'font_family'=> $font_family,'text'=> $my_text,'txt_color'=> $text_colour,'txt_height'=> $height,'txt_order'=> $text_order,'txt_rotation'=> $rotation,'txt_width'=> $width,'txt_x_pos'=> $x_position,'txt_y_pos'=> $y_position);

    $posts['data' = array('cat_id'=>$cat_id,'post_id'=>$post_id,'sticker_info'=>$sticker_info,'text_info'=>$text_info);

   $output[] = $posts;
}
$output['error'] = false; 

$output['message'] = 'Data Retrived successfull'; 

echo json_encode($output);
?>