问题
I would like to create custom formatted JSON to be used with NVD3.js, but am not sure how to create the nested arrays with PDO?
Example data tables:
+--------------+-------------------+---------------------+
| volume_name | volume_files_used | recorded |
+--------------+-------------------+---------------------+
| content001 | 130435938 | 2014-08-22 13:20:44 |
| content002 | 95977391 | 2014-08-22 13:20:44 |
+--------------+-------------------+---------------------+
Using PDO
and json_encode()
:
//JSON OUTPUT
$stmtJSON = $pdo->prepare("SELECT volume_name,
volume_files_used,
recorded FROM collection;");
$stmtJSON->execute();
$json = json_encode($stmtJSON->fetchAll(PDO::FETCH_ASSOC));
print_r($json);
Current JSON
output:
[
{
"volume_name": "content001",
"volume_files_used": "130435938",
"recorded": "2014-08-22 13:20:44"
},
{
"volume_name": "content002",
"volume_files_used": "95977391",
"recorded": "2014-08-22 13:20:44"
}
]
I would like the following JSON
output:
[
{
"key": "content001",
"values": [
{
"x": "2014-08-22 13:20:44",
"y": "130435938"
}
]
},
{
"key": "content002",
"values": [
{
"x": "2014-08-22 13:20:44",
"y": "95977391"
}
]
}
]
In the example above there are only two rows, but practically I would like to pull all values
for each volume_name
as key
:
[
{
"key": "content001",
"values": [
{
"x": "2014-08-22 13:20:44",
"y": "130435938"
},
{
"x": "2014-08-22 14:20:44",
"y": "130435940"
},
{
"x": "2014-08-22 15:20:44",
"y": "130435945"
},
{
"x": "2014-08-22 16:20:44",
"y": "130435965"
}
]
},
{
"key": "content002",
"values": [
{
"x": "2014-08-22 13:20:44",
"y": "95977391"
},
{
"x": "2014-08-22 14:20:44",
"y": "95977402"
},
{
"x": "2014-08-22 15:20:44",
"y": "95977445"
},
{
"x": "2014-08-22 16:20:44",
"y": "95977457"
}
]
}
]
ANSWER UPDATES
Charlotte Dunois updated output:
{
"content002": {
"values": [
{
"y": "95583732",
"x": "2014-08-27 11:05:01"
},
{
"y": "95539534",
"x": "2014-08-27 12:05:01"
}
],
"key": "content002"
},
"content001": {
"values": [
{
"y": "130121075",
"x": "2014-08-27 11:05:01"
},
{
"y": "130131806",
"x": "2014-08-27 12:05:01"
}
],
"key": "content001"
}
}
WORKING ANSWER
I managed to get assistance from another Dev. The below code is working, but anyone feel free to comment if you can do it better.
//JSON OUTPUT
$stmtJSON = $pdo->prepare("SELECT volume_name,
volume_files_used,
recorded FROM collection;");
$stmtJSON->execute();
$result = $stmtJSON->fetchAll(PDO::FETCH_ASSOC));
$temp_array = array();
foreach($result as $bf) {
if (!isset($temp_array[$bf['volume_name']])) {
$temp_array[$bf['volume_name']] = array();
}
$temp_array[$bf['volume_name']][] = array(
"x" => $bf['recorded'],
"y" => $bf['volume_files_used']);
}
$final_array = array();
foreach ($temp_array as $volume_name => $values) {
$final_array[] = array(
'key' => $volume_name,
'values' => $values);
}
$json = json_encode($final_array);
回答1:
You have to create a new array with this structure before encoding it as json object. This would do the job (your new formatted array is in $new_array, so you can just json encode that):
$new_array = array();
foreach($pdo_response as $bf) {
if(empty($new_array[$bf['volume_name']])) {
$new_array[$bf['volume_name']] = array("key" => $bf['volume_name'], "values" => array());
}
$new_array[$bf['volume_name']]['values'][] = array("x" => $bf['recoreded'], "y" => $bf['volume_files_used']);
}
Make use of array_values() if you want numeric keys (0 - ....) for the first dimension.
来源:https://stackoverflow.com/questions/25557428/custom-formatted-json-from-mysql-pdo-for-use-in-nvd3-js