将数组分组并合并为一个数组

时间:2016-09-12 11:29:48

标签: php arrays grouping

我有一个数组。如何对关键主动和被动进行分组和组合?

array(8) { 
    [0]=> array(4) { ["status"]=> string(3) "ONE" ["active"]=> string(9) "103347.02" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-01" } 
    [1]=> array(4) { ["status"]=> string(3) "TWO" ["active"]=> string(5) "65.32" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-01" } 
    [2]=> array(4) { ["status"]=> string(5) "THREE" ["active"]=> string(7) "3258.39" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-01" } 
    [3]=> array(4) { ["status"]=> string(4) "FOUR" ["active"]=> string(8) "35470.01" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-01" } 
    [4]=> array(4) { ["status"]=> string(3) "ONE" ["active"]=> string(9) "152685.68" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-02" } 
    [5]=> array(4) { ["status"]=> string(3) "TWO" ["active"]=> string(7) "1739.33" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-02" } 
    [6]=> array(4) { ["status"]=> string(5) "THREE" ["active"]=> string(7) "1300.00" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-02" } 
    [7]=> array(4) { ["status"]=> string(4) "FOUR" ["active"]=> string(8) "31461.06" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-02" } 
    [8]=> array(4) { ["status"]=> string(3) "ONE" ["active"]=> string(1) " " ["passive"]=> string(4) "2236" ["date"]=> string(10) "2016-08-01" }   
    [9]=> array(4) { ["status"]=> string(5) "THREE" ["active"]=> string(1) " " ["passive"]=> string(4) "4496" ["date"]=> string(10) "2016-08-02" } 
}

预期结果:

array(6) { 
    [0]=> array(4) { ["status"]=> string(3) "ONE" ["active"]=> string(9) "103347.02" ["passive"]=> string(4) "2236" ["date"]=> string(10) "2016-08-01" } 
    [1]=> array(4) { ["status"]=> string(3) "TWO" ["active"]=> string(5) "65.32" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-01" } 
    [2]=> array(4) { ["status"]=> string(5) "THREE" ["active"]=> string(7) "3258.39" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-01" } 
    [3]=> array(4) { ["status"]=> string(4) "FOUR" ["active"]=> string(8) "35470.01" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-01" } 
    [4]=> array(4) { ["status"]=> string(3) "ONE" ["active"]=> string(9) "152685.68" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-02" } 
    [5]=> array(4) { ["status"]=> string(3) "TWO" ["active"]=> string(7) "1739.33" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-02" } 
    [6]=> array(4) { ["status"]=> string(5) "THREE" ["active"]=> string(7) "1300.00" ["passive"]=> string(1) "4496" ["date"]=> string(10) "2016-08-02" } 
    [7]=> array(4) { ["status"]=> string(4) "FOUR" ["active"]=> string(8) "31461.06" ["passive"]=> string(1) " " ["date"]=> string(10) "2016-08-02" } 
}

2 个答案:

答案 0 :(得分:1)

最简单的解决方案是按日期重新索引,并将数字加在一起。你可以在php中键入juggle字符串。

$output = [];
foreach($array as $item){
    if(!isset($output[$item['date']])){
        $output[$item['date']] = $item;
    } else {
        $output[$item['date']]['active'] += $item['active'];
        $output[$item['date']]['passive'] += $item['passive'];
    }
}

//You can optionally remove the date keys again with:
$output = array_values($output);

答案 1 :(得分:0)

这是获得输出的另一种方式。

  $input=array( 
    0=> array( "status"=> "ONE" ,"active"=>  "103347.02","passive"=>" " ,"date"=> "2016-08-01" ),
    1=> array( "status"=> "TWO" ,"active"=> "65.32" ,"passive"=> " " ,"date"=> "2016-08-01" ),
    2=> array("status"=> "THREE" ,"active"=> "3258.39" ,"passive"=> " " ,"date"=>"2016-08-01" ),
    3=> array( "status"=> "FOUR" ,"active"=> "35470.01" ,"passive"=> " " ,"date"=>"2016-08-01" ),
    4=> array( "status"=> "ONE" ,"active"=> "152685.68" ,"passive"=> " " ,"date"=> "2016-08-02" ),
    5=> array( "status"=> "TWO" ,"active"=> "1739.33" ,"passive"=> " " ,"date"=> "2016-08-02" ),
    6=> array( "status"=> "THREE" ,"active"=> "1300.00" ,"passive"=> " " ,"date"=> "2016-08-02" ),
    7=> array( "status"=> "FOUR" ,"active"=> "31461.06" ,"passive"=>  " " ,"date"=>  "2016-08-02" ),
    8=> array( "status"=> "ONE" ,"active"=>  " " ,"passive"=> "2236" ,"date"=> "2016-08-01"),   
    9=> array( "status"=> "THREE" ,"active"=> " " ,"passive"=> "4496" ,"date"=>  "2016-08-02" )
);

foreach($input as $index_1 => $value_1){
    if(ctype_space($value_1['active']) && !ctype_space($value_1['passive'])) {
        foreach($input as $index_2 => $value_2){
            if($value_2['status']==$value_1['status'] && ctype_space($value_2['passive'])){
                $input[$index_2]['passive']=$value_1['passive'];
                unset($input[$index_1]);
                break; 
            }
        }
    }
}
var_dump($input);