複数の多次元配列値をマージする効率的なアルゴリズムを探しています
例:
$a = array(
array(
'province'=>'ss',
'package' => 配列 (
配列(
) 'FX58'=>0,
'PName' => 'a'
),
),
'ストア名' => 'テスト ',
),
array(
'province'=>'cc',
'パッケージ' => array(
array(
' FX58'=> 1,
'PName' => 'b'
),
),
'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(<br /> array(<br /> 'province'=>'ss',<br /> 'package' => array(<br /> array(<br /> 'FX58'=>0,<br /> 'PName' => 'a'<br /> ),<br /> ),<br /> 'storeName'=> 'test',<br /> ),<br /> array(<br /> 'province'=>'cc',<br /> 'package' => array(<br /> array(<br /> 'FX58'=>1,<br /> 'PName' => 'b'<br /> ),<br /> ),<br /> 'storeName'=> 'test2',<br /> )<br />);<br />$b = array(<br /> array(<br /> 'province'=>'ss',<br /> 'package' => array(<br /> array(<br /> 'FX58'=>1,<br /> 'PName' => 'a'<br /> ),<br /> ),<br /> 'storeName'=> 'test',<br /> ),<br /> array(<br /> 'province'=>'cc',<br /> 'package' => array(<br /> array(<br /> 'FX58'=>2,<br /> 'PName' => 'b'<br /> ),<br /> ),<br /> 'storeName'=> 'test2',<br /> )<br />);<br /><br />foreach($a as $v) {<br /> foreach($v['package'] as $t) {<br /> $res[$v['province']][$v['storeName']][$t['PName']] = $t['FX58'];<br /> }<br />}<br />foreach($b as &$v) {<br /> foreach($v['package'] as $t) {<br /> $res[$v['province']][$v['storeName']][$t['PName']] += $t['FX58'];<br /> }<br />}<br />foreach($a as &$v) {<br /> foreach($v['package'] as &$t) {<br /> $t['FX58'] = $res[$v['province']][$v['storeName']][$t['PName']];<br /> }<br />}<br /><br />print_r($a);<br />
Array<br />(<br /> [0] => Array<br /> (<br /> [province] => ss<br /> [package] => Array<br /> (<br /> [0] => Array<br /> (<br /> [FX58] => 1<br /> [PName] => a<br /> )<br /><br /> )<br /><br /> [storeName] => test<br /> )<br /><br /> [1] => Array<br /> (<br /> [province] => cc<br /> [package] => Array<br /> (<br /> [0] => Array<br /> (<br /> [FX58] => 3<br /> [PName] => b<br /> )<br /><br /> )<br /><br /> [storeName] => test2<br /> )<br /><br />)<br /><br />
<br />$a = array(<br /> array(<br /> 'province'=>'ss',<br /> 'package' => array(<br /> array(<br /> 'FX58'=>0,<br /> 'PName' => 'a'<br /> ),<br /> ),<br /> 'storeName'=> 'test',<br /> ),<br /> array(<br /> 'province'=>'cc',<br /> 'package' => array(<br /> array(<br /> 'FX58'=>1,<br /> 'PName' => 'b'<br /> ),<br /> ),<br /> 'storeName'=> 'test2',<br /> )<br />);<br />$b = array(<br /> array(<br /> 'province'=>'ss',<br /> 'package' => array(<br /> array(<br /> 'FX58'=>1,<br /> 'PName' => 'a'<br /> ),<br /> ),<br /> 'storeName'=> 'test',<br /> ),<br /> array(<br /> 'province'=>'cc',<br /> 'package' => array(<br /> array(<br /> 'FX58'=>2,<br /> 'PName' => 'b'<br /> ),<br /> ),<br /> 'storeName'=> 'test2',<br /> )<br />);<br /><br /><br />foreach($b as $k=>$v){<br /> for($i=0,$len=count($a); $i<$len; $i++){<br /> if($a[$i]['province'] == $v['province'] && $a[$i]['package'][0]['PName']==$v['package'][0]['PName'] && $a[$i]['storeName']==$v['storeName']){<br /> $a[$i]['package'][0]['FX58'] += $v['package'][0]['FX58'];<br /> }<br /> }<br />}<br /><br />echo '<pre class="brush:php;toolbar:false">';<br />print_r($a);<br />echo '';
<br />Array<br />(<br /> [0] => Array<br /> (<br /> [province] => ss<br /> [package] => Array<br /> (<br /> [0] => Array<br /> (<br /> [FX58] => 1<br /> [PName] => a<br /> )<br /><br /> )<br /><br /> [storeName] => test<br /> )<br /><br /> [1] => Array<br /> (<br /> [province] => cc<br /> [package] => Array<br /> (<br /> [0] => Array<br /> (<br /> [FX58] => 3<br /> [PName] => b<br /> )<br /><br /> )<br /><br /> [storeName] => test2<br /> )<br /><br />)<br />