如何从二维数组重组合并成新的数组?
示例:[{"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"
] 展开
[
{
"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)
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)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
题目:有以下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;
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;
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$arr = []; // 二维数组
$temp = []; // 临时数组,用于保存数据
foreach ($arr as $value) {
$temp = array_merge($arr, array_filter($value));
}
var_dump($temp); // $temp 即为所有value值合并后的数据,相同key元素的值最后的替换前面的;
$temp = []; // 临时数组,用于保存数据
foreach ($arr as $value) {
$temp = array_merge($arr, array_filter($value));
}
var_dump($temp); // $temp 即为所有value值合并后的数据,相同key元素的值最后的替换前面的;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询