1 回答
TA贡献1799条经验 获得超6个赞
你很接近。您只需要在每个循环中引用键和第一个userSummaries,而不是使用整个...
$myArr = json_decode($response, true);
$result_arr = ["userSummaries"=>[]];
foreach($myArr as $user) {
$result_arr["userSummaries"][] = $user['userSummaries'][0];
}
echo json_encode($result_arr);
结果是:
{"userSummaries":[
{"id":"9910","status":"Active","name":"Jhon"},
{"id":"8754","status":"Active","name":"Jane"}
]}
如果您预见每个中的 userSummaries 本身将有多个用户......那么这将起作用:
$response ='[
{
"userSummaries": [
{
"id": "9910",
"status": "Active",
"name": "Jhon"
}
]
},
{
"userSummaries": [
{
"id": "8754",
"status": "Active",
"name": "Jane"
},
{
"id": "5421",
"status": "Active",
"name": "Bob"
}
]
}
]';
$myArr = json_decode($response, true);
$result_arr = ["userSummaries"=>[]];
foreach($myArr as $usergroup) {
foreach($usergroup['userSummaries'] as $user) {
$result_arr["userSummaries"][] = $user;
}
}
echo json_encode($result_arr);
结果是:
{"userSummaries":[
{"id":"9910","status":"Active","name":"Jhon"},
{"id":"8754","status":"Active","name":"Jane"},
{"id":"5421","status":"Active","name":"Bob"}
]}
- 1 回答
- 0 关注
- 126 浏览
添加回答
举报