求高效率算法,合并多个多维数组值

WBOY
Release: 2016-06-13 12:20:05
Original
958 people have browsed it

求高效算法,合并多个多维数组值
例如
$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(<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 />
Copy after login
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 />
Copy after login

------解决思路----------------------
<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 '
Copy after login
';


<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 />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template