普通网友 2015-02-20 14:08
浏览 37
已采纳

带有Json问题的PHP数组

I want to achieve this using my php script:

{  
    "aaData":[  
        [  
            "100",
            "189",
            "2"
        ],
        [  
            "100",
            "189",
            "1"
        ],
        [  
            "100",
            "188",
            "4"
        ],
        [  
            "100",
            "188",
            "3"
        ],
        [  
            "100",
            "188",
            "2"
        ]
    ]
}

I am fetching data from database, and i want to put results to above format. Query looks like this:

SELECT user_id, module_id, action_id FROM test;

Please help me to put results of these 3 columns on format which i mentioned above.

Regards

  • 写回答

1条回答 默认 最新

  • dongmei1988 2015-02-20 14:14
    关注

    Using PDO, you can do:

    $result = $conn->execute("SELECT `user_id`, `module_id`, `action_id` FROM `test`");
    $rows = $result->fetchAll(PDO::FETCH_NUM);
    echo json_encode(array('aaData' => $rows));
    

    fetchAll returns a 2-dimensional array of the results of the query. The PDO::FETCH_NUM mode makes the rows of these results be numerically-indexed arrays. Then we put this into the aaData element of another array, and convert it to JSON.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退