簡體   English   中英

如何返回 json object 與 json 數組里面?

[英]How to return json object with json array inside of it?

我有很多這樣的關系:服務器版本: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 function 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)
...

或者你可以只是...

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

我還想添加類別所以這就是我帶來的

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM