如何在数组的嵌套对象中基于排序连接数据我有这样的数据对象:
{
"data": [
{
"id": "1",
},
{
"id": "2",
},
],
"includes": {
"users": [
{
"name": "Ronni Herdi"
},
{
"name": "Floridamaya2"
},
],
"tweets": [
{
"created_at": "2021-02-26T01:08:42.000Z"
},
{
"created_at": "2021-02-26T01:46:27.000Z"
}
]
}
}
如何基于排序加入数据使数据数组在一个数组中,我想要这样的结果:
{
"data": [
{
"id": "1",
"name": "Ronni Herdi",
"created_at": "2021-02-26T01:08:42.000Z"
},
{
"id": "2",
"name": "Floridamaya2",
"created_at": "2021-02-26T01:46:27.000Z"
}
]
}
我该怎么做...?
只需遍历数据并将项目附加到新的关联数组:
$input_json = ''; // json from post
$input = json_decode($input_json, true);
$joined = [];
foreach($input['data'] as $i => $item) {
$joined[] = array_merge(
$item,
$input['includes']['users'][$i],
$input['includes']['tweets'][$i]
);
}
$output = ['data' => $joined];
print_r($output);
http://sandbox.onlinephpfunctions.com/code/feb743acd25f4dfa9674d18b74d9d2a5d6b6a78f
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句