Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在给定的结构中输出json?_Php_Json_Codeigniter - Fatal编程技术网

Php 如何在给定的结构中输出json?

Php 如何在给定的结构中输出json?,php,json,codeigniter,Php,Json,Codeigniter,我希望通过这种方式获得给定格式的json输出 "free_issue": [ { "product_id": [ "44", "45", "95" ], "free_product_ids": [ "15", "118" ], "structure": { "req_

我希望通过这种方式获得给定格式的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; 
$product_id=array();
$free_product_id=array();
$free_issue=array();
$sql=“从tbl\U eligibleitem\u中选择eff.eff\u id、eff.eff\u product\u typeid、eff.eff\u数量、eff.eff\u freeProduct\u id、eff.qt作为eff free as eff,其中eff.eff\u sdate eff\u id和ff.ff\u status='E';
$query=$this->db->query($sql3);
如果($query->num\u rows()>0)
{
foreach($query->result()作为$row2)
{
$sql4=“从tbl\U产品中选择tp.product\U id作为tp,其中tp.product\U type\U id=$row->eff\U product\U typeid和tp.VALUE\U id=$row2->id\U ff\U VALUE\U id”;
$query2=$this->db->query($sql4);
foreach($query2->result()作为$row3)
{
$product\U id[]=$row3->product\U id;
}
}
}
其他的
{
$sql4=“从tbl\U产品中选择tp.product\U id作为tp,其中tp.product\U type\U id=$row->eff\U product\U typeid”;
$query2=$this->db->query($sql4);
foreach($query2->result()作为$row3)
{
$product\U id[]=$row3->product\U id;
}
}
$sql5=“从tbl_flavor_free中选择ff.id_ff_flavor_id作为ff,其中ff.ff_active=1和ff.ff_product_type_id=$row->eff_id和ff.ff_status='F';
$fquery=$this->db->query($sql5);
如果($fquery->num\u rows()>0)
{
foreach($fquery->result()作为$frow2)
{
$sql6=“从tbl\U产品中选择tp.product\U id作为tp,其中tp.product\U type\U id=$row->eff\U freeProduct\U id和tp.VALUE\U id=$frow2->id\U ff\U VALUE\U id”;
$fquery2=$this->db->query($sql6);
foreach($fquery2->result()作为$frow3)
{
$free_product_id[]=$frow3->product_id;
}
}
}
其他的
{
$sql6=“从tbl\U产品中选择tp.product\U id作为tp,其中tp.product\U type\U id=$row->eff\U freeProduct\U id”;
$fquery2=$this->db->query($sql6);
foreach($fquery2->result()作为$frow3)
{
$free_product_id[]=$frow3->product_id;
}
}
$quantity=数组(
“请求数量”=>$row->eff数量,
“自由数量”=>美元行->数量
);
$free_issue[]=数组(
“产品id”=>$product\U id,
“免费产品标识”=>免费产品标识,
“结构”=>$数量);
}           
退回$free_发行;
我想不出这里的问题。
请任何人帮我整理一下?

数组$free\u product\u id和$product\u id在循环结束时没有重置,因此它们仍然包含它们的值

你需要重新设置它们

$free_product_id = $product_id = array();

在此修改代码

谢谢!!!!!!!!!!!!!!!!!!!非常感谢…我得到了正确的数据博士再次感谢你