2016-09-12 12 views
0

配列があります。アクティブなキーとパッシブなキーをグループ化して組み合わせるにはどうすればよいですかグループ化して1つの配列にまとめる

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" } 
} 
+0

大規模に使用している場合は、SQLIte3などのデータベースを使用することを検討してください。 – SOFe

+0

ところで、あなたのvar_dumpで何が問題になっていますか?最初の出力には10個の要素があり、2番目の出力には8個の要素があります。 – SOFe

答えて

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); 
+0

ありがとうございます。その仕事 – user2787192

1

最も簡単な解決策は、日付によって、それのインデックスを再作成すること、そして一緒に数字を追加します。あなたは、PHPで文字列をジャグリングすることができます。

$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); 
関連する問題