如何让json出现在给定的结构中?

时间:2015-05-19 00:52:27

标签: php json codeigniter

我希望以这种方式获得给定格式的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; 

我无法在这里弄清楚问题。 请任何人帮我解决一下?

1 个答案:

答案 0 :(得分:3)

数组$ free_product_id和$ product_id在循环结束时没有被重置,所以它们仍然包含它们的值。

您需要重置它们

$free_product_id = $product_id = array();

此处修改了代码http://pastebin.com/dTDahRFP