Custom formatted JSON from MYSQL PDO for use in NVD3.js

旧巷老猫 提交于 2019-12-06 15:38:06

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!