【问题标题】:How to format nested json with data of multiple tables如何用多个表的数据格式化嵌套的json
【发布时间】:2015-07-16 06:51:31
【问题描述】:

我正在尝试从以一对多关系相互映射的三个表中获取 json 数据,并希望以这种格式显示这些数据

{
    products: [
        {
            "product_id": 121,
            "name": "Nike Fusion",
            "type": "Running Shoe",
            "brand": "Nike",
            "product_Description": "very good",
            "size": {
                      "value": "small"
                      "price": 200$
                    },
            "weight": {
                        "value": "100gm"
                        "price": 100$
                      }
       },
       {

       },
       ...
    ]
}

在这张表中的“品牌”“产品描述”和“耐克”“非常好” 从第二张桌子。 我正在编写这个 php 文件来实现这一点

<?php
include('db_connection.php');

 $result = mysql_query("select product_name,product_id from products where product_id='1' "); 

    $json_response = array(); //Create an array
    while ($row = mysql_fetch_array($result))
    {
        $row_array = array();
       // $row_array['product_name'] = $row['product_name'];        


        $qus_pk = $row['product_id'];  

        $option_qry = mysql_query("select ss.Name,sv.value_s from specifications ss,specification_value sv where sv.specification_ID=ss.specification_ID and sv.product_id =$qus_pk");
        while ($opt_fet = mysql_fetch_array($option_qry))
        {
            $row_array[] = array(
                 $opt_fet['Name'] => $opt_fet['value_s'],

            );

        }
        array_push($json_response, $row_array); //push the values in the array
    }

    echo json_encode($json_response);

?>

但我得到这样的输出

[
    [
        {
            "brand": "Nike"
        },
        {
            "product_Description": "very good"
        }

    ]
]

【问题讨论】:

    标签: php mysql arrays json nested


    【解决方案1】:

    而不是您的以下代码:

        $row_array = array();
       // $row_array['product_name'] = $row['product_name'];        
    
    
        $qus_pk = $row['product_id'];  
    
        $option_qry = mysql_query("select ss.Name,sv.value_s from specifications ss,specification_value sv where sv.specification_ID=ss.specification_ID and sv.product_id =$qus_pk");
        while ($opt_fet = mysql_fetch_array($option_qry))
        {
            $row_array[] = array($opt_fet['Name'] => $opt_fet['value_s'],);
    
        }
    
    
        //array_push($json_response, $row_array);
    

    要像上面那样显示,您必须一次性添加产品描述,您可以通过以下方式实现: $row_array = 数组(); // $row_array['product_name'] = $row['product_name'];

        $qus_pk = $row['product_id'];  
        $product_desc = '';
        $brand_name = '';
    
        $option_qry = mysql_query("select ss.Name,sv.value_s from specifications ss,specification_value sv where sv.specification_ID=ss.specification_ID and sv.product_id =$qus_pk");
        while ($opt_fet = mysql_fetch_array($option_qry))
        {
            //$row_array[] = array(
                 $opt_fet['Name'] => $opt_fet['value_s'], );
            $product_desc .=$opt_fet['value_s'].",";
            $brand_name .= $opt_fet['Name'].",";
    
        }
        $product_desc = rtrim($product_desc);
        $brand_name = rtrim($brand_name);
        $row_array['product_Description'] = $product_desc;
        $row_array['brand'] = $brand_name;
        array_push($json_response, $row_array); //push the values in the array
    

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      相关资源
      最近更新 更多