如何从二维数组重组合并成新的数组?

示例:[{"name":"kkkk","hospital_num":"qwe","outpatient_num":"","attendance_num":"","card... 示例:
[
{
"name": "kkkk",
"hospital_num": "qwe",
"outpatient_num": "",
"attendance_num": "",
"card": "kkkk",
"birthday": "kkkk",
"phone": "kkkk",
"gender": 1,
"address": "kkkk"
},
{
"name": "kkkk",
"hospital_num": "qwe",
"outpatient_num": "",
"attendance_num": "",
"card": "kkkk",
"birthday": "kkkk",
"phone": "kkkk",
"gender": 1,
"address": "kkkk"
},
{
"name": "kkkk",
"hospital_num": "",
"outpatient_num": "aaaa",
"attendance_num": "aaaa",
"card": "kkkk",
"birthday": "kkkk",
"phone": "kkkk",
"gender": 1,
"address": "kkkk"
}
]
想重组成这样的数组,该怎么做:
[
"name": "kkkk",
"hospital_num": "qwe",
"outpatient_num": "aaaa",
"attendance_num": "aaaa",
"card": "kkkk",
"birthday": "kkkk",
"phone": "kkkk",
"gender": 1,
"address": "kkkk"
]
展开
 我来答
匿名用户
2020-09-15
展开全部
给你给思路吧, 遍历对象数组, 然后将每个对象中值为空的属性删掉, 然后用 Object.assign()来合并数组中的对象(已经删掉了空值属性)
const data = [
{
"name": "kkkk",
"hospital_num": "qwe",
"outpatient_num": "",
"attendance_num": "",
"card": "kkkk",
"birthday": "kkkk",
"phone": "kkkk",
"gender": 1,
"address": "kkkk"
},
{
"name": "kkkk",
"hospital_num": "qwe",
"outpatient_num": "",
"attendance_num": "",
"card": "kkkk",
"birthday": "kkkk",
"phone": "kkkk",
"gender": 1,
"address": "kkkk"
},
{
"name": "kkkk",
"hospital_num": "",
"outpatient_num": "aaaa",
"attendance_num": "aaaa",
"card": "kkkk",
"birthday": "kkkk",
"phone": "kkkk",
"gender": 1,
"address": "kkkk"
}
]
let result = {}
for(const d of data){
Object.keys(d).forEach(p => {
if(d[p] === ''){
delete d[p]
}
})
Object.assign(result, d)
}
console.log(result)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
快乐权御天下
2020-09-15 · TA获得超过2.9万个赞
知道大有可为答主
回答量:1.6万
采纳率:95%
帮助的人:517万
展开全部
题目:有以下2个二维数组

1
$a=Array(0 => Array(id => 66,class_name => www.iiwnet.com),1 => Array(id => 67,class_name => linux ));
2
$b=Array(0 => Array(class_count=> 8),1 => Array(class_count => 2));
两个数组内容如上,如何把$a和$b合并成为一个新的二维数组,要求新数组内容如下:

1
Array(0 => Array(id => 66,class_name => www.iiwnet.com,class_count=> 8),1 => Array(id => 67,class_name => linux ,class_count => 2));
这个题目一定有人遇到过 正好今天群里有人问 就给出2种解法
以下代码由 PHP教程网 原创提供
01
方法一:
02

03
function arrpreg(){
04
$a=Array(0 => Array(id => 66,class_name => www.iiwnet.com),1 => Array(id => 67,class_name => linux ));
05
$b=Array(0 => Array(class_count=> 8),1 => Array(class_count => 2));
06
$arr = array();
07
foreach ($a as $k => $r) {
08
foreach($r as $k1 => $r1){
09
$arr[$k][$k1] = $r1;
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
shenkebiao
2020-09-16 · TA获得超过605个赞
知道小有建树答主
回答量:367
采纳率:100%
帮助的人:150万
展开全部
$arr = []; // 二维数组

$temp = []; // 临时数组,用于保存数据
foreach ($arr as $value) {
$temp = array_merge($arr, array_filter($value));
}
var_dump($temp); // $temp 即为所有value值合并后的数据,相同key元素的值最后的替换前面的;
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式