Rumah > pembangunan bahagian belakang > tutorial php > 求高效算法,合并多个多维数组值

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

WBOY
Lepaskan: 2016-06-23 13:34:32
asal
910 orang telah melayarinya

例如
$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数组中


回复讨论(解决方案)

这样?

$new=array_merge($a,$b);echo "<pre class="brush:php;toolbar:false">";print_r($new);echo "
Salin selepas log masuk
Salin selepas log masuk
";

分组键是哪个?

这样?

$new=array_merge($a,$b);echo "<pre class="brush:php;toolbar:false">";print_r($new);echo "
Salin selepas log masuk
Salin selepas log masuk
";

额 我需要的是将相同province,storeName,,pName中FX158数据统计起来

分组键是哪个?

将相同province,storeName,,pName中FX158数据统计起来

$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);
Salin selepas log masuk
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        ))
Salin selepas log masuk
Salin selepas log masuk

$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<$len; $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 '<pre class="brush:php;toolbar:false">';print_r($a);echo '
Salin selepas log masuk
';

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        ))
Salin selepas log masuk
Salin selepas log masuk

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan