1 回答

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊
你很接近。您只需要在每個(gè)循環(huán)中引用鍵和第一個(gè)userSummaries,而不是使用整個(gè)...
$myArr = json_decode($response, true);
$result_arr = ["userSummaries"=>[]];
foreach($myArr as $user) {
$result_arr["userSummaries"][] = $user['userSummaries'][0];
}
echo json_encode($result_arr);
結(jié)果是:
{"userSummaries":[
{"id":"9910","status":"Active","name":"Jhon"},
{"id":"8754","status":"Active","name":"Jane"}
]}
如果您預(yù)見每個(gè)中的 userSummaries 本身將有多個(gè)用戶......那么這將起作用:
$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);
結(jié)果是:
{"userSummaries":[
{"id":"9910","status":"Active","name":"Jhon"},
{"id":"8754","status":"Active","name":"Jane"},
{"id":"5421","status":"Active","name":"Bob"}
]}
- 1 回答
- 0 關(guān)注
- 162 瀏覽
添加回答
舉報(bào)