php - 如何获取给定结构中的 json 输出?

标签 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; 

我无法弄清楚这里的问题。 请问有人可以帮我解决一下吗?

最佳答案

数组 $free_product_id 和 $product_id 在循环结束时不会重置,因此它们仍然包含其值。

您需要重置它们

$free_product_id = $product_id = array();

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

关于php - 如何获取给定结构中的 json 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315069/

相关文章:

php - TinyMVC PDO 层在数据库查询时引发错误

php - 如何使用 get_user_meta() 从数组中获取数据

javascript - 创建和循环 json 对象时出现问题

json - 推特 API 错误 215

php - 如何获取ajax值并存储在PHP变量中?

php - 从单独的文件发送错误

javascript - 计算多个字段的值并在 Javascript 中显示每一行

javascript - 下拉列表值不存储在数据库中

php - 时区、夏令时等

java - struts2-rest-插件 : sending json data to PUT/POST