$arr1=Array([0] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 30),
[1] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 26),
[2] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 10));
$arr2=Array([0] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 26),
[1] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 10),
[2] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 30));
二个二维数组,根据system_gid参数来判断,如果相等就把$arr2相等那条数组插到$arr1那条数据下。合并出来如$arr3 ,请大神支招,O(∩_∩)O谢谢!
$arr3=Array([0] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 30),
[1] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 30),
[2] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 26),
[3] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 26),
[4] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 10),
[5] => Array(
[fee] => 212.45
[system_single] => 0.00
[system_gid] => 10));
PHP 二维数组根据条件合并
答案:1 悬赏:70 手机版
解决时间 2021-03-11 04:13
- 提问者网友:难遇难求
- 2021-03-10 03:31
最佳答案
- 五星知识达人网友:鱼忧
- 2021-03-10 04:47
function mergById($arr1, $arr2, $id){
$tmp = array();
foreach($arr2 as $record){
$tmp[$record[$id]][] = $record;
}
$arr3 = array();
foreach($arr1 as $record){
$arr3[] = $record;
if(isset($tmp[$record[$id]])){
foreach($tmp[$record[$id]] as $sameIdRecord){
$arr3[] = $sameIdRecord;
}
}
}
return $arr3;
}
$tmp = array();
foreach($arr2 as $record){
$tmp[$record[$id]][] = $record;
}
$arr3 = array();
foreach($arr1 as $record){
$arr3[] = $record;
if(isset($tmp[$record[$id]])){
foreach($tmp[$record[$id]] as $sameIdRecord){
$arr3[] = $sameIdRecord;
}
}
}
return $arr3;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯