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

WBOY
Freigeben: 2016-06-13 12:20:05
Original
957 Leute haben es durchsucht

求高效算法,合并多个多维数组值
例如
$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 />
Nach dem Login kopieren
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 />
Nach dem Login kopieren

------解决思路----------------------
<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 '
Nach dem Login kopieren
';


<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 />
Nach dem Login kopieren

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage