求高效率算法,合并多个多维数组值
程序员文章站
2022-05-03 18:57:53
...
求高效算法,合并多个多维数组值
例如
$a = array(
array(
'province'=>'ss',
'package' => array(
array(
'FX58'=>0,
'PName' => 'a'
),
),
'storeName'=> 'test',
),
array(
'province'=>'cc',
'package' => array(
array(
'FX58'=>1,
'PName' => 'b'
),
),
'storeName'=> 'test2',
)
);
$b = array(
array(
'province'=>'ss',
'package' => array(
array(
'FX58'=>1,
'PName' => 'a'
),
),
'storeName'=> 'test',
),
array(
'province'=>'cc',
'package' => array(
array(
'FX58'=>2,
'PName' => 'b'
),
),
'storeName'=> 'test2',
)
);
将b数组值统计到a数组中
------解决思路----------------------
------解决思路----------------------
例如
$a = array(
array(
'province'=>'ss',
'package' => array(
array(
'FX58'=>0,
'PName' => 'a'
),
),
'storeName'=> 'test',
),
array(
'province'=>'cc',
'package' => array(
array(
'FX58'=>1,
'PName' => 'b'
),
),
'storeName'=> 'test2',
)
);
$b = array(
array(
'province'=>'ss',
'package' => array(
array(
'FX58'=>1,
'PName' => 'a'
),
),
'storeName'=> 'test',
),
array(
'province'=>'cc',
'package' => array(
array(
'FX58'=>2,
'PName' => 'b'
),
),
'storeName'=> 'test2',
)
);
将b数组值统计到a数组中
------解决思路----------------------
$a = array(
array(
'province'=>'ss',
'package' => array(
array(
'FX58'=>0,
'PName' => 'a'
),
),
'storeName'=> 'test',
),
array(
'province'=>'cc',
'package' => array(
array(
'FX58'=>1,
'PName' => 'b'
),
),
'storeName'=> 'test2',
)
);
$b = array(
array(
'province'=>'ss',
'package' => array(
array(
'FX58'=>1,
'PName' => 'a'
),
),
'storeName'=> 'test',
),
array(
'province'=>'cc',
'package' => array(
array(
'FX58'=>2,
'PName' => 'b'
),
),
'storeName'=> 'test2',
)
);
foreach($a as $v) {
foreach($v['package'] as $t) {
$res[$v['province']][$v['storeName']][$t['PName']] = $t['FX58'];
}
}
foreach($b as &$v) {
foreach($v['package'] as $t) {
$res[$v['province']][$v['storeName']][$t['PName']] += $t['FX58'];
}
}
foreach($a as &$v) {
foreach($v['package'] as &$t) {
$t['FX58'] = $res[$v['province']][$v['storeName']][$t['PName']];
}
}
print_r($a);
Array
(
[0] => Array
(
[province] => ss
[package] => Array
(
[0] => Array
(
[FX58] => 1
[PName] => a
)
)
[storeName] => test
)
[1] => Array
(
[province] => cc
[package] => Array
(
[0] => Array
(
[FX58] => 3
[PName] => b
)
)
[storeName] => test2
)
)
------解决思路----------------------
$a = array(
array(
'province'=>'ss',
'package' => array(
array(
'FX58'=>0,
'PName' => 'a'
),
),
'storeName'=> 'test',
),
array(
'province'=>'cc',
'package' => array(
array(
'FX58'=>1,
'PName' => 'b'
),
),
'storeName'=> 'test2',
)
);
$b = array(
array(
'province'=>'ss',
'package' => array(
array(
'FX58'=>1,
'PName' => 'a'
),
),
'storeName'=> 'test',
),
array(
'province'=>'cc',
'package' => array(
array(
'FX58'=>2,
'PName' => 'b'
),
),
'storeName'=> 'test2',
)
);
foreach($b as $k=>$v){
for($i=0,$len=count($a); $i if($a[$i]['province'] == $v['province'] && $a[$i]['package'][0]['PName']==$v['package'][0]['PName'] && $a[$i]['storeName']==$v['storeName']){
$a[$i]['package'][0]['FX58'] += $v['package'][0]['FX58'];
}
}
}
echo '';';
print_r($a);
echo '
Array
(
[0] => Array
(
[province] => ss
[package] => Array
(
[0] => Array
(
[FX58] => 1
[PName] => a
)
)
[storeName] => test
)
[1] => Array
(
[province] => cc
[package] => Array
(
[0] => Array
(
[FX58] => 3
[PName] => b
)
)
[storeName] => test2
)
)
相关文章
相关视频