带内连接的json数组

时间:2017-06-27 09:26:07

标签: php mysql json inner-join

我希望我的json响应像这样

cols = ['PERIOD_START_TIME','ID']
df = df.sort_values(cols).groupby(cols, as_index=False).first()
print (df)
     PERIOD_START_TIME  ID  A  VALUE
0  06.01.2017 02:00:00  55  8     35
1  06.01.2017 02:00:00  65  8     10
2  06.01.2017 03:00:00  55  8     63
3  06.01.2017 03:00:00  65  8     22
4  06.01.2017 04:00:00  55  8     63
5  06.01.2017 04:00:00  65  8     12

但我得到的是这个

{
subtotal="null",
create time ="2017-06-25 11:35:50",

products:[{product name:first,
          price:55}

]}

这是我的PHP脚本

[{subtotal="null", create time ="2017-06-25 11:35:50",

product name:first ,price:55},

]

我是初学者,我希望你能帮助我 提前谢谢

3 个答案:

答案 0 :(得分:0)

请查看此代码此json数据存储所有订单详情。

 $data = array();
 $final_data = array();
 $products = array();
   while($row=mysqli_fetch_assoc($res)){
    $data['subtotal'] = $row['subtotal'];
    $data['create time'] = $row['create time'];
    $products['product name'] = $row['product name'];
    $products['price'] = $row['price'];

   $data['products'] = $products;

   $final_data[] = $data;

 }

echo json_encode($final_data, JSON_UNESCAPED_UNICODE);

$conn->close();

答案 1 :(得分:0)

尝试以这种方式定义$ readsarray

$i=0;
while($row =mysqli_fetch_assoc($result))
{
$readsarray[$i]["subtotal"] = $row["subtotal"];
$readsarray[$i]["create time"]=$row["create time"];
$readsarray[$i]["products"]=array('product  name'=>$row["product name"],'price'=>$row["price"]);
$i++;
}
 echo json_encode($readsarray,true);

    $conn->close();
?>

答案 2 :(得分:0)

请检查这个答案。

$data = array();
$final_data = array();
$products = array();
 while($row=mysqli_fetch_assoc($res)){
  $data['subtotal'] = $row['subtotal'];
  $data['create time'] = $row['create time'];
  $products['product name'] = $row['product name'];
  $products['price'] = $row['price'];

 $data['products'][] = $products;

 $final_data[] = $data;

}

echo json_encode($final_data, JSON_UNESCAPED_UNICODE);

$conn->close();