在php中编译嵌套的JSON数组

时间:2016-03-22 18:39:32

标签: php arrays json

我想发送嵌套的JSON数组。它包含以下格式。

${"posts":[{
    "abc":"123",
    "xyz":"123",
    "pro":[{
        "name":"Brinjal",
        "qty":"500 gms"
    },
    {
        "name":"Brinjal",
        "qty":"500 gms"
    }] 
}]
}

这是我的php代码:

$rows = $stmt->fetchAll();
if ($rows) {
     $order["posts"]   = array();
    foreach ($rows as $row) {
        $post = array();
        $post["order_id"] =  $row["order_id"];
        $post["order_totalamount"] = $row["order_totalamount"];
        $post["address"] = $row["address"];
        $post["pincode"] = $row["pincode"];
        $post["delivery_timeslot"] = $row["delivery_timeslot"];
        $post["order_date"] = $row["order_date"];
        $query1= "query";
        $rows1 = $stmt->fetchAll();
        if ($rows1) {
            foreach ($rows1 as $row1) {                             
                $query2= "query";
                $rows2 = $stmt->fetchAll();
                $post["product"]   = array();   
                if ($rows2) {
                    $products = array();
                    foreach ($rows2 as $row2) {
                        $products["product_name"]=$row2["product_name"];
                        $products["prod_qty"] = $row2["product_minquantity"];
                    }
                    array_push($post["product"],$products);
                    echo json_encode($products);
                }

            }
          }
        array_push($order["posts"], $post);
    }  
    echo json_encode($order);   
}

从上面的代码我得到了结果:

$ {"posts":
    [{
    "order_id":"18",
    "order_totalamount":"40",
    "address":"2, Chetan Society, Vadodara",
    "pincode":"390023",
    "delivery_timeslot":"Zone wise delivery",
    "order_date":"2016-03-18 17:50:53",
    "product":[{"product_name":"Brinjal","prod_qty":"500 gms"}]
    }]
}

但我的实际产品数组是:

${"product_name":"Banana","prod_qty":"1 Kg"}{"product_name":"Brinjal","prod_qty":"500 gms"}

请帮忙。我被困在上面了。尝试了很多,但没有成功。

1 个答案:

答案 0 :(得分:-1)

            foreach ($rows2 as $row2) {
                $products["product_name"]=$row2["product_name"];
                $products["prod_qty"] = $row2["product_minquantity"];
            }

您使用上一个$products["product_name"]的值覆盖$products["prod_qty"]$row2

{"product_name":"Banana","prod_qty":"1 Kg"}{"product_name":"Brinjal","prod_qty":"500 gms"}实际上由两个回声组成:

  1. {"product_name":"Banana","prod_qty":"1 Kg"}
  2. {"product_name":"Brinjal","prod_qty":"500 gms"}
  3. 但你没有看到差异。我建议在它们之间回显一个字符串,或使用var_dump()之类的字符串来查看你回显的值。

    您应该执行以下操作:

            for ($i = 0; $i < count($rows2); $i++) {
                $products[$i]["product_name"]=$rows2[$i]["product_name"];
                $products[$i]["prod_qty"] = $rows2[$i]["product_minquantity"];
            }
    

    这将导致:[{"product_name":"Banana","prod_qty":"1 Kg"},{"product_name":"Brinjal","prod_qty":"500 gms"}]

    仅供参考$array[] =array_push()具有相同的结果,但调用array_push()的开销较小。

    修改 下面是一个完整的例子,因为它似乎不适合你:

    //Demo $order
    $order = array (
        'posts' => 
        array (
            0 => 
            array (
                'order_id' => '18',
                'order_totalamount' => '40',
                'address' => '2, Chetan Society, Vadodara',
                'pincode' => '390023',
                'delivery_timeslot' => 'Zone wise delivery',
                'order_date' => '2016-03-18 17:50:53',
            ),
        ),
    );
    
    //Demo $rows2
    $rows2 = array(
        array(
            'product_name' => 'banana', 
            'product_minquantity' => '1 Kg'
        ),
        array(
            'product_name' => 'Brinjal', 
            'product_minquantity' => '500 gms'
        )
    );
    
    $post['product'] = array();   
    if ($rows2) {
        $products = array();
        for ($i = 0; $i < count($rows2); $i++) {
            $products[$i]['product_name'] = $rows2[$i]['product_name'];
            $products[$i]['prod_qty']     = $rows2[$i]['product_minquantity'];
        }
        $post['product'] = $products;
        echo json_encode($products); //[{"product_name":"banana","prod_qty":"1 Kg"},{"product_name":"Brinjal","prod_qty":"500 gms"}]
    }
    //$order['posts'][] = $post;
    $order['posts'][0] = $order['posts'][0] + $post;
    //In this example I only have 1 item in $order['posts'] so I know to add to key 0.
    //In your case you need to know which key of $rows1 you need to do the adding.
    //Change the foreach loop of $rows to a for loop as well so you can define your own keynames ($i).
    echo '<br>==================================================================<br>';
    echo json_encode($order);
    /*
        {
        "posts":[
          {
             "order_id":"18",
             "order_totalamount":"40",
             "address":"2, Chetan Society, Vadodara",
             "pincode":"390023",
             "delivery_timeslot":"Zone wise delivery",
             "order_date":"2016-03-18 17:50:53",
             "product":[
                {
                   "product_name":"banana",
                   "prod_qty":"1 Kg"
                },
                {
                   "product_name":"Brinjal",
                   "prod_qty":"500 gms"
                }
             ]
          }
       ]
    }
    */