我想以这种方式获取给定格式的 json 输出。
"free_issue": [
{
"product_id": [
"44",
"45",
"95"
],
"free_product_ids": [
"15",
"118"
],
"structure": {
"req_qty": "12",
"free_qty": "1"
}
}
]
第一个数组没问题。但从第二个产品 ID 和免费产品 ID 开始重复,如下所示。
"free_issue": [
{
"product_id": [
"44",
"45",
"95"
],
"free_product_ids": [
"15",
"118"
],
"structure": {
"req_qty": "12",
"free_qty": "1"
}
},
{
"product_id": [
**"44",
"45",
"95",**
"20",
"21",
"22",
"23",
"32",
"33",
"48",
"49",
"51",
"52"
],
"free_product_ids": [
**"15",
"118"**
],
"structure": {
"req_qty": "0",
"free_qty": "0"
}
},
{
"product_id": [
**"44",
"45",
"95",
"20",
"21",
"22",
"23",
"32",
"33",
"48",
"49",
"51",
"52",**
"20",
"21",
"22",
"23",
"32",
"33",
"48",
"51"
],
"free_product_ids": [
"15",
"118",
"53",
"54",
"55"
],
"structure": {
"req_qty": "12",
"free_qty": "1"
}
},
{
"product_id": [
"44",
"45",
"95",
"20",
"21",
"22",
"23",
"32",
"33",
"48",
"49",
"51",
"52",
"20",
"21",
"22",
"23",
"32",
"33",
"48",
"51",
"20",
"21",
"22",
"23",
"32",
"33",
"48",
"49",
"51",
"52"
],
"free_product_ids": [
"15",
"118",
"53",
"54",
"55",
"20",
"21",
"22",
"23",
"32",
"33",
"48"
],
"structure": {
"req_qty": "25",
"free_qty": "2"
}
}
]
上面代码中的粗体数据被重复。 这是我使用的查询。
$product_id = array();
$free_product_id = array();
$free_issue = array();
$sql = "select eff.eff_id,eff.eff_product_typeid,eff.eff_qty,eff.eff_freeProduct_id,eff.qty from tbl_eligibleitem_for_free as eff where eff.eff_sdate <= CURDATE() and eff.`status` = 1";
$this->db->mod_select($sql);
$try = $this->db->query($sql);
foreach ($try->result() as $row)
{
$rowid = $row->eff_id;
$sql3 = "select ff.id_ff_flavor_id from tbl_flavor_free as ff where ff.ff_active = 1 and ff.ff_product_type_id = $row->eff_id and ff.ff_status = 'E'";
$query = $this->db->query($sql3);
if($query->num_rows()>0)
{
foreach ($query->result() as $row2)
{
$sql4 = "select tp.product_id from tbl_product as tp where tp.product_type_id = $row->eff_product_typeid and tp.flavour_id = $row2->id_ff_flavor_id";
$query2 = $this->db->query($sql4);
foreach ($query2->result() as $row3)
{
$product_id[] = $row3->product_id;
}
}
}
else
{
$sql4 = "select tp.product_id from tbl_product as tp where tp.product_type_id = $row->eff_product_typeid";
$query2 = $this->db->query($sql4);
foreach ($query2->result() as $row3)
{
$product_id[] = $row3->product_id;
}
}
$sql5 = "select ff.id_ff_flavor_id from tbl_flavor_free as ff where ff.ff_active = 1 and ff.ff_product_type_id = $row->eff_id and ff.ff_status = 'F'";
$fquery = $this->db->query($sql5);
if($fquery->num_rows()>0)
{
foreach ($fquery->result() as $frow2)
{
$sql6 = "select tp.product_id from tbl_product as tp where tp.product_type_id = $row->eff_freeProduct_id and tp.flavour_id = $frow2->id_ff_flavor_id";
$fquery2 = $this->db->query($sql6);
foreach ($fquery2->result() as $frow3)
{
$free_product_id[] = $frow3->product_id;
}
}
}
else
{
$sql6 = "select tp.product_id from tbl_product as tp where tp.product_type_id = $row->eff_freeProduct_id";
$fquery2 = $this->db->query($sql6);
foreach ($fquery2->result() as $frow3)
{
$free_product_id[] = $frow3->product_id;
}
}
$quantity = array(
'req_qty'=>$row->eff_qty,
'free_qty'=>$row->qty
);
$free_issue[] = array(
'product_id'=>$product_id,
'free_product_ids'=>$free_product_id,
'structure'=>$quantity);
}
return $free_issue;
我无法弄清楚这里的问题。 请问有人可以帮我解决一下吗?
最佳答案
数组 $free_product_id 和 $product_id 在循环结束时不会重置,因此它们仍然包含其值。
您需要重置它们
$free_product_id = $product_id = array();
关于php - 如何获取给定结构中的 json 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315069/