你好,我有印刷JSONPHP JSON沒有顯示所有相關的數值
<?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);
?>
和該輸出這個PHP代碼是
{
"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"
}
}
}
}
你可以做 '$ vari_array [$ VAR [0] [] = $ VAR [1]。'現在大小是多維數組。 – WisdmLabs
感謝它解決了我的問題:) –