我有一个 json 响应,如下所示:
$response ='[
{
"userSummaries": [
{
"id": "9910",
"status": "Active",
"name": "Jhon"
}
]
},
{
"userSummaries": [
{
"id": "8754",
"status": "Active",
"name": "Jane"
}
]
}
]';
我想用userSummaries这个 php 代码将其分组:
$myArr = json_decode($response, true);
$result_arr = [];
array_walk($myArr,function($v,$k) use (&$result_arr){
$result_arr[key($v)] = $v[key($v)];
});
echo json_encode($result_arr);
并且响应只返回一个数据:
{"userSummaries":[{"id":"8754","status":"Active","name":"Jane"}]}
是否可以得到这样的输出响应?:
{"userSummaries":[{"id":"9910","status":"Active","name":"Jhon"}, {"id":"8754","status":"Active","name":"Jane"}, ]}
通过网络尝试过,但没有找到解决方案
这是我的脚本: https: //3v4l.org/tVkK5
也尝试过这个:
$class_array = array();
foreach ($myArr as $sa) {
$class_array[$sa['userSummaries']][] = array('name' => $sa['name']);
}
但返回:
Notice: Undefined index: name in /in/hvSFC on line 28
Warning: Illegal offset type in /in/hvSFC on line 28
Notice: Undefined index: name in /in/hvSFC on line 28
Warning: Illegal offset type in /in/hvSFC on line 28
[]
需要帮忙
哈士奇WWW