Php 包括';键'&';价值';json响应的属性

Php 包括';键'&';价值';json响应的属性,php,mysql,json,d3.js,Php,Mysql,Json,D3.js,查询DB表,获取数组,并写入json文件 $sql2 = "SELECT * FROM omharddown ORDER BY id DESC LIMIT 1"; $result2 = mysqli_query($dbc, $sql2); $json_response = array(); while ($row = mysqli_fetch_array($result2, MYSQL_ASSOC)) { $row_array 'key:'['ING_SW_CB'] = $row '

查询DB表,获取数组,并写入json文件

$sql2 = "SELECT * FROM omharddown ORDER BY id DESC LIMIT 1";
$result2 = mysqli_query($dbc, $sql2);

$json_response = array();

while ($row = mysqli_fetch_array($result2, MYSQL_ASSOC)) {
    $row_array 'key:'['ING_SW_CB'] = $row 'value:'['ING_SW_CB'];
    $row_array 'key:'['SB_SW_CB'] = $row 'value:'['SB_SW_CB'];
    $row_array 'key:'['NG3_SW_CB'] = $row 'value:'['NG3_SW_CB'];
    $row_array 'key:'['Mould_Close'] = $row 'value:'['Mould_Close'];
    $row_array 'key:'['Leak_Test'] = $row 'value:'['Leak_Test'];
    $row_array 'key:'['ML_Load'] = $row 'value:'['ML_Load'];
    $row_array 'key:'['Pre_Heat'] = $row 'value:'['Pre_Heat'];
    $row_array 'key:'['Dispense'] = $row 'value:'['Dispense'];
    $row_array 'key:'['A310'] = $row 'value:'['A310'];
    $row_array 'key:'['Gelation'] = $row 'value:'['Gelation'];
    $row_array 'key:'['Platen'] = $row 'value:'['Platen'];
    $row_array 'key:'['Mainline_Unload'] = $row 'value:'['Mainline_Unload'];
    $row_array 'key:'['De_mould'] = $row 'value:'['De_mould'];
    $row_array 'key:'['Clean_Up'] = $row 'value:'['Clean_Up'];
    $row_array 'key:'['Soda_Blast'] = $row 'value:'['Soda_Blast'];
    $row_array 'key:'['Miscellaneous'] = $row 'value:'['Miscellaneous'];

    //push the values in the array
    array_push($json_response,$row_array);
}
    //echo json_encode($json_response);
    $json_data = json_encode($json_response, JSON_PRETTY_PRINT);
    file_put_contents('your_json_file.json', $json_data); 
json文件中的输出如下:

[[{"ING_SW_CB":"5","SB_SW_CB":"3",.........}]]
我要找的是:

[[{"key":"ING_SW_CB", "value":"5", "key":"SB_SW_CB", "value":3,....}]]
有人有解决办法吗

我创建了一个使用外部json数据的D3图表。 图表接受y值,但我需要弄清楚如何使用x轴上的类别名称。 用于填充图表的代码

barData=[]

    d3.json("your_json_file.json", function (data) {

     for (key in data) {
         barData.push(data[key].ING_SW_CB)
         barData.push(data[key].SB_SW_CB)
         barData.push(data[key].NG3_SW_CB)
         barData.push(data[key].Mould_Close)
         barData.push(data[key].Leak_Test)
         barData.push(data[key].ML_Load)
         barData.push(data[key].Pre_Heat)
         barData.push(data[key].Dispense)
         barData.push(data[key].A310)
         barData.push(data[key].Gelation)
         barData.push(data[key].Platen)
         barData.push(data[key].Mainline_Unload)
         barData.push(data[key].De_mould)
         barData.push(data[key].Clean_Up)
         barData.push(data[key].Soda_Blast)
         barData.push(data[key].Miscellaneous)
         // add more .push if needed
     }

因此,如果您的数据如下所示:

var current_data = [{
    "ING_SW_CB": "5",
    "SB_SW_CB": "3",
    "NG3_SW_CB": "2",
    "Mould_Close": "8",
    "Leak_Test": "6",
    "ML_Load": "2",
    "Pre_Heat": "1"
}]
。。。您可以使用将其转换为评论中提到的格式。尝试:

var formatted_data = d3.entries(current_data[0]);
这将为您提供:


所需格式无效,因为在同一对象中有多个
键,这是不可能的。它需要使用您当前拥有的格式或
[[[{“key”:“ING_SW_CB”,“value”:“5”},{“key”:“SB_SW_CB”,“value”:3,…..}]
为什么要使用该格式的JSON?您正在有效地删除键及其值之间的关联,并依靠不同键/值对的顺序来重新建立它。我在D3中创建了一个图表,需要外部json文件中的数据来填充。我见过的最好的例子使用了类似的格式。您可能希望它采用@Sean建议的格式,该格式通过将键/值对分离为对象来保留关联。谢谢你们的回复。我如何以[[{“key”:“ING_SW_CB”,“value”:“5”},{“key”:“SB_SW_CB”,“value”:3,…..}]]的形式写入文件我会在调用外部json文件的函数中使用d3.entries吗?