Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何美化Rest API响应对象上的联接结果?_Php_Mysql_Json_Rest_Api - Fatal编程技术网

Php 如何美化Rest API响应对象上的联接结果?

Php 如何美化Rest API响应对象上的联接结果?,php,mysql,json,rest,api,Php,Mysql,Json,Rest,Api,我开始学习使用PHP作为核心语言构建RESTAPI,并开始讨论如何创建响应对象。我的MySQL数据库中有表用户和用户社区。我试图实现的是使响应对象如下所示: { id, name, age, social: [ twitter, facebook, github ] } 我的sql查询当前如下所示: select users.id, users.name, users.age, user_social.*

我开始学习使用PHP作为核心语言构建RESTAPI,并开始讨论如何创建响应对象。我的MySQL数据库中有表用户和用户社区。我试图实现的是使响应对象如下所示:

{
    id,
    name,
    age,
    social: [
        twitter,
        facebook,
        github
    ]
}
我的sql查询当前如下所示:

select users.id, users.name, users.age, user_social.* from users inner join user_social on users.id = user_social.user_id limit 10;
{
    id,
    name,
    age,
    twitter,
    facebook,
    github
}
目前我的JSON响应如下所示:

select users.id, users.name, users.age, user_social.* from users inner join user_social on users.id = user_social.user_id limit 10;
{
    id,
    name,
    age,
    twitter,
    facebook,
    github
}

所以问题是,我怎样才能将连接结果分组成我想要的结构?是否有MySQL方法,或者我应该通过循环PHP中的每个记录来映射它,然后发送响应。

MySQL只返回行和字段。你应该用PHP重新格式化它。

用户社交表是什么样子的?
id、用户id、twitter、facebook、github