【问题标题】:How to return json object with json array inside of it?如何返回其中包含 json 数组的 json 对象?
【发布时间】: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"
  }, 

【问题讨论】:

  • 按颜色聚合并按所有其他列分组

标签: php mysql sql json mariadb


【解决方案1】:

有两种方法:

  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)
...

或者你可以……

  1. 查询 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"
    ]
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-02
      • 2015-07-02
      • 2012-09-04
      • 2021-11-30
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多