PHP json没有显示所有相关值

时间:2015-07-22 06:38:40

标签: php json mysqli

您好我有这个用于打印json的PHP代码

<?php

include('databaseconnect.php');

$sql = "SELECT product_id,product_name FROM products WHERE product_id='1'";
$result = $conn->query($sql);

$sql2 = "SELECT GROUP_CONCAT(ss.Name,':',sv.value_s ) as Specifications FROM specifications ss, specification_value sv WHERE sv.specification_ID = ss.specification_ID AND sv.product_id =  '1'";
$fetch = $conn->query($sql2);

$sql3 = "select GROUP_CONCAT(v.name,':',vv.value) as variants from variant v,variant_value vv where v.variant_id=vv.variant_id and product_id='1'";
$fetch1 = $conn->query($sql3);


$json['products'] = array();

while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){

    $json['products'] = $row;

  }

$json['products']['specification'] = array();

while ($row = mysqli_fetch_assoc($fetch)){

    $specification_array  = explode(',', $row["Specifications"]);
    $speci_array = array();
    foreach($specification_array as $spec){
        $spec = explode(':',$spec);
        $speci_array[$spec[0]] = $spec[1];
    }
    $json['products']['specification'] = $speci_array;

    //array_push($json['products'],$row_temp);
   }

$json['products']['specification']['variants'] = array();

while ($row = mysqli_fetch_assoc($fetch1)){

    $variants_array  = explode(',', $row["variants"]);
    $vari_array = array();
    foreach($variants_array as $var){
        $var = explode(':',$var);
        $vari_array[$var[0]] = $var[1];
    }
    $json['products']['specification']['variants'] = $vari_array;

    //array_push($json['products'],$row_temp);
   }


echo Json_encode($json);

?>

并输出

{
    "products": {
        "product_id": "1",
        "product_name": "Face Wash",
        "specification": {
            "brand": "Python",
            "product_Description": "very good",
            "variants": {
                "size": "long"
            }
        }
    }
}

但在此我还有一个变量值,即 “尺寸”:“小”而且没有出现。 抱歉英语不好,请在回答之前向我询问任何说明

我想要的输出

{
    "products": {
        "product_id": "1",
        "product_name": "Face Wash",
        "specification": {
            "brand": "Python",
            "product_Description": "very good",
            "variants": {
                "size": "small"
                "size": "long"
            }
        }
    }
}

4 个答案:

答案 0 :(得分:2)

您无法两次添加相同的密钥size。之前的密钥会被覆盖。

答案 1 :(得分:1)

由于您再次重复键protected override void OnCreate (Bundle SaveInstace) { base.OnCreate (SaveInstace); SetContentView (Resource.Layout.ListView_layout); ActionBar.SetDisplayHomeAsUpEnabled (true); //Gets ListView object instance Database sqldb1 = ((GlobalClass)this.Application).sqldb; listItems = FindViewById<ListView> (Resource.Id.listItems); GetCursorView (); } void GetCursorView() { Database sqldb1 = ((GlobalClass)this.Application).sqldb; Android.Database.ICursor sqldb_cursor = sqldb1.GetRecordCursor(); if (sqldb_cursor != null) { sqldb_cursor.MoveToFirst (); string[] from = new string[] {"_id","date","Value"}; int[] to = new int[] { Resource.Id.ListRow1, Resource.Id.ListRow2, Resource.Id.ListRow3 }; //Creates a SimplecursorAdapter for ListView object SimpleCursorAdapter sqldb_adapter = new SimpleCursorAdapter (this, Resource.Layout.record_view, sqldb_cursor, from, to); sqldb_adapter.ViewBinder = new MyCustomView(); listItems.Adapter = sqldb_adapter; } else { } } public class MyCustomView:Java.Lang.Object, SimpleCursorAdapter.IViewBinder { public bool SetViewValue (View view, Android.Database.ICursor cursor, int i) { if (view.Id == Resource.Id.ListRow3) { // If the column is IS_STAR then we use custom view. int is_val = cursor.GetInt (i); CheckBox cb = (CheckBox) view; if (is_val != 0) { // set the visibility of the view to GONE cb.Checked = true; return true; } else { return true; } // For others, we simply return false so that the default binding // happens. } return true; } } ,因此值将被覆盖。传递属于数组中相同Key的所有必需值。

答案 2 :(得分:0)

是的,你不能使用相同的密钥名称.. 你可以得到像

{
    "products": {
        "product_id": "1",
        "product_name": "Face Wash",
        "specification": {
            "brand": "Python",
            "product_Description": "very good",
            "variants": [{"size": "small"},{"size": "long"}]
        }
    }
}

答案 3 :(得分:0)

您可以执行'$ vari_array [$ var [0]] [] = $ var [1];'现在的大小是多维数组。 如https://stackoverflow.com/users/1993125/wisdmlabs在评论中所示