我正在尝试为WordPress中自行开发的API创建REST响应。当前代码为
$response = array();
$response[] = array(
\'version\' => \'1.0\',
\'user\' => array(
\'first_name\' => $user->first_name,
\'last_name\' => $user->last_name,
\'email\' => $user->user_email,
\'id\' => $user->ID,
),
);
return rest_ensure_response( $response );
返回(我正在使用用户对象)
[
{
"version": "1.0",
"user": {
"first_name": "Test",
"last_name": "User",
"email": "[email protected]",
"id": 19
}
}
]
但是我需要这个表格
[
{
"version": "1.0",
"user": [
{
"first_name": "Test",
"last_name": "User",
"email": "[email protected]",
"id": 19,
}
]
}
]
因此,这里的挑战是如何让[]绕过;用户?“;?
这很可能是一个一般性问题,因为我无法添加;[“和”]"E;手动发送到响应数组
最好的,托马斯
SO网友:Razon Komar Pal
只需使用另一个array()
内部用户array()
$response = array();
$response[] = array(
\'version\' => \'1.0\',
\'user\' => array(
array(
\'first_name\' => \'Razon\',
\'last_name\' => \'komar pal\',
\'email\' => \'[email protected]\',
\'id\' => 10,
)
),
);
return rest_ensure_response($response);
输出如下:
[
{
"version": "1.0",
"user": [
{
"first_name": "Razon",
"last_name": "komar pal",
"email": "[email protected]",
"id": 10
}
]
}
]
希望这对您有用:)