php两个二维数组根据某个字段相同重新组成新的数组

这里,我要实现的是两个二维数组,根据某一个字段相同,将其他的数据相加

数组一:

$data1 = array( 
		   0=>array( "days"=>"2018-10-11","IOS"=>"3", "Android"=>"92"),
		   1=>array( "days"=>"2018-10-10", "IOS"=>"103" ,"Android"=>"569" ),
		   2=>array( "days"=>"2018-10-09" ,"IOS"=>"234", "Android"=>"568" ) ,
		   3=>array( "days"=> "2018-10-08" ,"IOS"=> "222" ,"Android"=> "548" ) ,
		   4=>array( "days"=> "2018-10-07" ,"IOS"=> "230" ,"Android"=> "736" ) ,
		   5=>array( "days"=> "2018-10-06" ,"IOS"=> "656", "Android"=> "885" ) ,
		   6=>array( "days"=> "2018-10-05" ,"IOS"=> "333" ,"Android"=> "894" ) ,
	);

数组二:

$data2 = array( 
		   0=>array("days"=> "2018-10-10","IOS"=> "1","Android"=> "2") ,
		   1=>array("days"=> "2018-10-09" ,"IOS"=> "5" ,"Android"=> "5" ) ,
		   2=>array("days"=> "2018-10-08" ,"IOS"=> "2" ,"Android"=> "4" ) ,
		   3=>array("days"=> "2018-10-07" ,"IOS"=> "2" ,"Android"=> "10" ) ,
		   4=>array("days"=> "2018-10-06" ,"IOS"=> "8" ,"Android"=> "14" ) ,
		   5=>array("days"=> "2018-10-05" ,"IOS"=> "2" ,"Android"=> "16" ) ,
	);

根据days相同将IOS和Android分别相加

我们先将两个数组合并:

$data = array_merge($data1,$data2);

结果:

Array
(
    [0] => Array
        (
            [days] => 2018-10-11
            [IOS] => 3
            [Android] => 92
        )

    [1] => Array
        (
            [days] => 2018-10-10
            [IOS] => 103
            [Android] => 569
        )

    [2] => Array
        (
            [days] => 2018-10-09
            [IOS] => 234
            [Android] => 568
        )

    [3] => Array
        (
            [days] => 2018-10-08
            [IOS] => 222
            [Android] => 548
        )

    [4] => Array
        (
            [days] => 2018-10-07
            [IOS] => 230
            [Android] => 736
        )

    [5] => Array
        (
            [days] => 2018-10-06
            [IOS] => 656
            [Android] => 885
        )

    [6] => Array
        (
            [days] => 2018-10-05
            [IOS] => 333
            [Android] => 894
        )

    [7] => Array
        (
            [days] => 2018-10-10
            [IOS] => 1
            [Android] => 2
        )

    [8] => Array
        (
            [days] => 2018-10-09
            [IOS] => 5
            [Android] => 5
        )

    [9] => Array
        (
            [days] => 2018-10-08
            [IOS] => 2
            [Android] => 4
        )

    [10] => Array
        (
            [days] => 2018-10-07
            [IOS] => 2
            [Android] => 10
        )

    [11] => Array
        (
            [days] => 2018-10-06
            [IOS] => 8
            [Android] => 14
        )

    [12] => Array
        (
            [days] => 2018-10-05
            [IOS] => 2
            [Android] => 16
        )

)

再对这个合并后的数组进行处理

$newdata = [];
foreach($data as $k=>$v){
    if(!isset($newdata[$v['days']])){
        $newdata[$v['days']] = $v;
    }else{
        $newdata[$v['days']]['IOS'] += $v['IOS'];
        $newdata[$v['days']]['Android'] += $v['Android'];
    }
}

结果:打印  p(array_values($newdata));

Array
(
    [0] => Array
        (
            [days] => 2018-10-11
            [IOS] => 3
            [Android] => 92
        )

    [1] => Array
        (
            [days] => 2018-10-10
            [IOS] => 104
            [Android] => 571
        )

    [2] => Array
        (
            [days] => 2018-10-09
            [IOS] => 239
            [Android] => 573
        )

    [3] => Array
        (
            [days] => 2018-10-08
            [IOS] => 224
            [Android] => 552
        )

    [4] => Array
        (
            [days] => 2018-10-07
            [IOS] => 232
            [Android] => 746
        )

    [5] => Array
        (
            [days] => 2018-10-06
            [IOS] => 664
            [Android] => 899
        )

    [6] => Array
        (
            [days] => 2018-10-05
            [IOS] => 335
            [Android] => 910
        )

)

 

这个就是我们想要的数组

 

 

你可能感兴趣的:(php)