PHP循环二维数组,将两个二维数组转换成一个二维数组
数组一:Array([0]=>Array([value]=>['价格','品牌','质量','售后服务','交货期','其他'][sub_id]=>1,2,3,4,5,6...
数组一:
Array (
[0] => Array (
[value] => ['价格 ','品牌','质量','售后服务','交货期 ','其他 ']
[sub_id] => 1,2,3,4,5,6
[name] => 一、您在采购装饰材料过程中考虑较多的因素是:
[topic_id] => 1 )
[1] => Array (
[value] => ['固定供应商','网络','同行推荐','销售厂商业务拜访','自己找的'] [sub_id] => 7,8,9,10,11
[name] => 二、您采购装饰材料的信息渠道主要来自哪里?
[topic_id] => 2 )
[2] => Array (
[value] => ['商业建筑','住宅小区','展览/样板房','特殊空间(如火车站,飞机场,医院等)','酒店或俱乐部','其他项目']
[sub_id] => 12,13,14,15,16,17
[name] => 三、您最近负责的工程是哪方面的?
[topic_id] => 3 )
[3] => Array (
[value] => ['装饰板,吊顶','石材','门窗,卫浴','五金','窗帘,布艺','灯具,照明 ','玻璃,瓷砖','地板,地毯','涂料','暖通,防水','幕墙,壁纸','工电料','其他']
[sub_id] => 18,19,20,21,22,23,24,25,26,27,28,29,30
[name] => 四、您最近工程项目需要采购以下哪些材料
[topic_id] => 4 )
[4] => Array (
[value] => ['有','没有','不确定']
[sub_id] => 31,32,33
[name] => 五、您在项目施工过程中有没有融资购买材料的需求?
[topic_id] => 5 )
)
数组二:
Array (
[0] => Array (
[sub_ids] => 2
[count] => 2 )
[1] => Array (
[sub_ids] => 3
[count] => 2 )
[2] => Array (
[sub_ids] => 9
[count] => 3 )
[3] => Array (
[sub_ids] => 10
[count] => 2 )
[4] => Array (
[sub_ids] => 11
[count] => 2 )
[5] => Array (
[sub_ids] => 13
[count] => 1 )
[6] => Array (
[sub_ids] => 15
[count] => 3 )
[7] => Array (
[sub_ids] => 17
[count] => 2 )
[8] => Array (
[sub_ids] => 19
[count] => 2 )
[9] => Array (
[sub_ids] => 21
[count] => 3 )
[10] => Array (
[sub_ids] => 24
[count] => 1 )
[11] => Array (
[sub_ids] => 25
[count] => 1 )
[12] => Array (
[sub_ids] => 26
[count] => 2 )
[13] => Array (
[sub_ids] => 30
[count] => 1 )
[14] => Array (
[sub_ids] => 31
[count] => 1 )
[15] => Array (
[sub_ids] => 34
[count] => 3 )
)
想要的结果:
Array (
[0] => Array (
[value] => ['价格 ','品牌','质量','售后服务','交货期 ','其他 ']
[sub_id] => 1,2,3,4,5,6
[name] => 一、您在采购装饰材料过程中考虑较多的因素是:
[topic_id] => 1
[count] => 0,2,2,0,0,0 )
[1] => Array (
[value] => ['固定供应商','网络','同行推荐','销售厂商业务拜访','自己找的']
[sub_id] => 7,8,9,10,11
[name] => 二、您采购装饰材料的信息渠道主要来自哪里?
[topic_id] => 2
[count] => 0,0,3,2,2 )
......) 展开
Array (
[0] => Array (
[value] => ['价格 ','品牌','质量','售后服务','交货期 ','其他 ']
[sub_id] => 1,2,3,4,5,6
[name] => 一、您在采购装饰材料过程中考虑较多的因素是:
[topic_id] => 1 )
[1] => Array (
[value] => ['固定供应商','网络','同行推荐','销售厂商业务拜访','自己找的'] [sub_id] => 7,8,9,10,11
[name] => 二、您采购装饰材料的信息渠道主要来自哪里?
[topic_id] => 2 )
[2] => Array (
[value] => ['商业建筑','住宅小区','展览/样板房','特殊空间(如火车站,飞机场,医院等)','酒店或俱乐部','其他项目']
[sub_id] => 12,13,14,15,16,17
[name] => 三、您最近负责的工程是哪方面的?
[topic_id] => 3 )
[3] => Array (
[value] => ['装饰板,吊顶','石材','门窗,卫浴','五金','窗帘,布艺','灯具,照明 ','玻璃,瓷砖','地板,地毯','涂料','暖通,防水','幕墙,壁纸','工电料','其他']
[sub_id] => 18,19,20,21,22,23,24,25,26,27,28,29,30
[name] => 四、您最近工程项目需要采购以下哪些材料
[topic_id] => 4 )
[4] => Array (
[value] => ['有','没有','不确定']
[sub_id] => 31,32,33
[name] => 五、您在项目施工过程中有没有融资购买材料的需求?
[topic_id] => 5 )
)
数组二:
Array (
[0] => Array (
[sub_ids] => 2
[count] => 2 )
[1] => Array (
[sub_ids] => 3
[count] => 2 )
[2] => Array (
[sub_ids] => 9
[count] => 3 )
[3] => Array (
[sub_ids] => 10
[count] => 2 )
[4] => Array (
[sub_ids] => 11
[count] => 2 )
[5] => Array (
[sub_ids] => 13
[count] => 1 )
[6] => Array (
[sub_ids] => 15
[count] => 3 )
[7] => Array (
[sub_ids] => 17
[count] => 2 )
[8] => Array (
[sub_ids] => 19
[count] => 2 )
[9] => Array (
[sub_ids] => 21
[count] => 3 )
[10] => Array (
[sub_ids] => 24
[count] => 1 )
[11] => Array (
[sub_ids] => 25
[count] => 1 )
[12] => Array (
[sub_ids] => 26
[count] => 2 )
[13] => Array (
[sub_ids] => 30
[count] => 1 )
[14] => Array (
[sub_ids] => 31
[count] => 1 )
[15] => Array (
[sub_ids] => 34
[count] => 3 )
)
想要的结果:
Array (
[0] => Array (
[value] => ['价格 ','品牌','质量','售后服务','交货期 ','其他 ']
[sub_id] => 1,2,3,4,5,6
[name] => 一、您在采购装饰材料过程中考虑较多的因素是:
[topic_id] => 1
[count] => 0,2,2,0,0,0 )
[1] => Array (
[value] => ['固定供应商','网络','同行推荐','销售厂商业务拜访','自己找的']
[sub_id] => 7,8,9,10,11
[name] => 二、您采购装饰材料的信息渠道主要来自哪里?
[topic_id] => 2
[count] => 0,0,3,2,2 )
......) 展开
4个回答
展开全部
数组合并函数是array_merge,但是仍然没看懂你要的结果是怎么得来的
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是想用数组1中的sub_id 和数组2中的sub_ids 关联,再组合出新的数组吧
不知道我这个我这段程序能不能满足你的需求
$data =array();
foreach($arr_1 as $key_1 => $value_1){
$data[$key_1] = $value_1;
if($value_1['sub_id']){
$ids = '';
$ids = explode(',' , $value_1['sub_id']);
$count_str = '';
foreach($ids as $k=>$v){
foreach($arr_2 as $key_2 => $value_2){
if($v == $value_2['sub_ids']){
$count_str[$k] = $value_2['count'];
break;
}else{
$count_str[$k] = '0';
}
}
}
$data[$key_1]['count'] = implode(',' , $count_str);
}
}
不知道我这个我这段程序能不能满足你的需求
$data =array();
foreach($arr_1 as $key_1 => $value_1){
$data[$key_1] = $value_1;
if($value_1['sub_id']){
$ids = '';
$ids = explode(',' , $value_1['sub_id']);
$count_str = '';
foreach($ids as $k=>$v){
foreach($arr_2 as $key_2 => $value_2){
if($v == $value_2['sub_ids']){
$count_str[$k] = $value_2['count'];
break;
}else{
$count_str[$k] = '0';
}
}
}
$data[$key_1]['count'] = implode(',' , $count_str);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不知道你合并之后那个count是那里来的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询