简体   繁体   English

PHP json没有显示所有相关值

[英]PHP json not showing all related values

Hello I have this php code for printing json 您好,我有这个用于打印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);

?>

and output of this is 这个的输出是

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

but in this i have one more value for variant ie "size":"small" and that is not showing up. 但是在这种情况下,我还有另一个变体值,即“ size”:“ small”,并且没有显示出来。 sorry for bad English please ask me for any clarification before answering 对不起,英语不好,请在回答前请我澄清一下

my desired output 我想要的输出

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

You can't add same key size twice. 您不能两次添加相同的密钥size The previous key gets overwritten by later. 之前的键将被以后的键覆盖。

Since you are repeating the key size again the values are being overwritten. 由于您再次重复密钥size ,因此值被覆盖。 Pass all the required values which are belonging to the same Key in an array. 传递数组中属于同一键的所有必需值。

yes you cant use same key name .. you can get like 是的,您不能使用相同的键名..您可能会喜欢

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

You can do '$vari_array[$var[0]][] = $var[1];' 您可以执行'$ vari_array [$ var [0]] [] = $ var [1];' now the size is multidimensional array. 现在的大小是多维数组。 as shown by https://stackoverflow.com/users/1993125/wisdmlabs in comments https://stackoverflow.com/users/1993125/wisdmlabs的注释所示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM