如何返回其中包含 json 数组的 json 对象?

Posted

技术标签:

【中文标题】如何返回其中包含 json 数组的 json 对象?【英文标题】:How to return json object with json array inside of it? 【发布时间】:2021-05-04 08:02:10 【问题描述】:

我有多对多的关系 像这样 : 服务器版本:10.4.17-MariaDB

表格颜色(ID、名称)。 表格项目(id、title....)。 表 item_color(id,items_id,color_id)。

我的查询是这样的:

SELECT items.*,colors.name FROM items,item_color,colors
 where
 items.id = item_color.item_id
 and
 colors.id = item_color.color_id

我使用 php 函数 json_encode()如何退货


    "id": "22",
    "title": "my products 515151",
    "descreption": "5454545455",
    "price": "0.05",
    "quantity": "2",
    "date_added": "2021-01-29 14:37:24",
    "primary_image": "http://localhost/ecomerce/uploads/1611927444hat.jpg",
    "color": [
      0 : "pink",
      1 : "white"
      ]

  

如果这样:

    "id": "22",
    "title": "my products 515151",
    "descreption": "5454545455",
    "price": "0.05",
    "quantity": "2",
    "date_added": "2021-01-29 14:37:24",
    "primary_image": "http://localhost/ecomerce/uploads/1611927444hat.jpg",
    "color": "pink"
  ,
  
    "id": "22",
    "title": "my products 515151",
    "descreption": "5454545455",
    "price": "0.05",
    "quantity": "2",
    "date_added": "2021-01-29 14:37:24",
    "primary_image": "http://localhost/ecomerce/uploads/1611927444hat.jpg",
    "color": "red"
  , 

【问题讨论】:

按颜色聚合并按所有其他列分组 【参考方案1】:

有两种方法:

    如果要保留查询,必须先解析数据,然后再将其解析为 json。
...
function regroup_color($data = array())
  $ret = array();
  foreach($data as $d)
    $id = $d['id'];
    if(!isset($ret[$id]))
      $color = $d['color'];
      unset($d['color']);
      $ret[$id] = $d;
      $ret[$id]['color'][] = $color;
    else
      $ret[$id]['color'][] = $d['color'];
    
  
  return $ret;

$data = regroup_color($data);
echo json_encode($data)
...

或者你可以……

    查询 2 部分,第一个是获取所有 items,第二个是获取 colors
...
$query = "SELECT * FROM items";
// get the data here using query above
$data = result of query;

foreach($data as $i => $d)
  $id = $d['id'];
  $query = "SELECT * FROM item_color JOIN colors ON item_color.color_id = colors.id";
  // get the data here using query above
  $colors = result of query;
  foreach($colors as color)
    $data[$i]['color'][] = $color['name'];
  

echo json_encode($data)
...

【讨论】:

【参考方案2】:

我还想添加类别,所以这就是我带来的

function regroup_color($data = array(),$data2 = array())
  // array that will hold our data and we will return it later
$ret = array();
// fetch the data as d
foreach($data as $d)
  //save the id 
  $id = $d['id'];
  //make sure no id was set
  if(!isset($ret[$id]))
    // save the name of the color
    $color = $d['color'];
    unset($d['color']);
    //save all the item data
    $ret[$id] = $d;
    // add color to color array
    $ret[$id]['color'][] = $color;
  else
    // if wa alredy did all the above things just keep adding colors to color array
    $ret[$id]['color'][] = $d['color'];
  

  
  
foreach($data2 as $d)

    //save the id 
      $id = $d['id'];
      //make sure no id was set
      if(!isset($ret[$id]))
        // save the name of the color
        $category = $d['category'];
        unset($d['category']);
        $ret[$id] = $d;
        $ret[$id]['category'][] = $category;
      else
        $ret[$id]['category'][] = $d['category'];
      
    

返回 $ret;

结果:

"22": 
"id": "22",
"title": "my products 515151",
"descreption": "5454545455",
"price": "0.05",
"quantity": "2",
"date_added": "2021-01-29 14:37:24",
"primary_image": "http://localhost/ecomerce/uploads/1611927444hat.jpg",
"color": [
  "black",
  "white",
  "pink",
  "red",
  "yellow"
],
"category": [
  "buttom",
  "hats",
  "shoes"
]

【讨论】:

以上是关于如何返回其中包含 json 数组的 json 对象?的主要内容,如果未能解决你的问题,请参考以下文章

如何解析包含多个相同类型的 JSON 对象(不是数组)的 JSON 对象

如何使用 Newtonsoft.Json 将包含数组数组的 json 对象解析为 C# 中的对象列表?

当对象包含其他对象数组时如何解析 JSON?

如何在 Angular 中将动态 JSON 对象数组导出为 CSV?

如何在 Postgres 中选择一列 json 对象,以便返回的行是 json 数组?

如何设置映射输入的嵌套 JSON 数组对象的状态