dongsetan3216 2014-06-23 11:51
浏览 30
已采纳

使用PHP构建JSON数据

I would like to produce the following JSON output using PHP

{
"dataid": "An optional sourcetable identifier",
"columns": [
    {
        "colvalue": "Status",
        "coltext": "Status",
        "header": "Status",
        "sortbycol": "Status",
        "groupbyrank": null,
        "pivot": true,
        "result": false
    },
    {
        "colvalue": "Priority",
        "coltext": "Priority",
        "header": "Priority",
        "sortbycol": "Priority",
        "dataid": "An optional id.",
        "groupbyrank": 4,
        "pivot": false,
        "result": false
    },
    {
        "colvalue": "Team",
        "coltext": "Team",
        "header": "Team",
        "sortbycol": false,
        "dataid": "An optional id.",
        "groupbyrank": 1,
        "pivot": false,
        "result": false
    },
    {
        "colvalue": "Module",
        "coltext": "Module",
        "header": "Module",
        "sortbycol": "Module",
        "dataid": "An optional id.",
        "groupbyrank": 2,
        "pivot": false,
        "result": false
    },
    {
        "colvalue": "SLA",
        "coltext": "SLA",
        "header": "SLA",
        "sortbycol": "SLA",
        "dataid": "An optional id.",
        "groupbyrank": 3,
        "pivot": false,
        "result": false
    },
    {
        "colvalue": "Score",
        "coltext": "Score",
        "header": "Score",
        "sortbycol": "Score",
        "groupbyrank": null,
        "pivot": false,
        "result": true
    }
],
"rows": [
    {
        "Team": "Technical",
        "Module": "BASIS",
        "SLA": "Critical",
        "Priority": "High",
        "Status": "In Progress",
        "Score": "1",
        "Score2": "18.27"
    },
    {
        "Team": "Technical",
        "Module": "BASIS",
        "SLA": "Critical",
        "Priority": "High",
        "Status": "In Progress",
        "Score": "1",
        "Score2": "18.27"
    },
 **** MORE ENTRIES ARE ITERATED BASED ON SQL ROW ***
]

}

However i am stuck on how to combine my array which are hardcoded and my dynamic data (taken from MySQL row).

The following is my PHP code to generate the column and dataid which is static

$test = array(
        "dataid" => 'An optional sourcetable identifier',
        "columns" => array(
            array(
                'colvalue' => 'Status',
                'coltext' =>'Status',
                'header' =>'Status',
                'sortbycol' => 'Status',
                'groupbyrank' => null,
                'pivot' => true,
                'result' => false
            ),
            array(
                'colvalue' => 'Priority',
                'coltext' => 'Priority',
                'header' => 'Priority',
                'sortbycol' => 'Priority',
                'dataid' => 'An optional id.',
                'groupbyrank' => 4,
                'pivot' => false,
                'result' => false
            ),
        )
    );

What i am stuck with is how do combine it with my row data? Can someone help me with this.

Thanks

  • 写回答

1条回答 默认 最新

  • dsiv4041 2014-06-23 11:59
    关注

    You can ammend existing arrays just by using a new key:

    $test['rows'] = $yourRowDataAsArray;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000