How To Change My Array Format In PHP -
my code generate array
array ( [0] => array ( [0] => 2015-05-28 [1] => 1 ) [1] => array ( [0] => 2015-05-29 [1] => 1 ) [2] => array ( [0] => 2015-06-02 [1] => 2 ) )
i want convert same bellow
array( [0] => array ( [0] => 05 [1] => 2 ) [1] => array ( [0] => 06 [1] => 2 ) );
how can convert array first month , total of second value
extract month key
, , check if month in output array exists or not.
<?php $array = array( array( '2015-05-28', 1 ), array( '2015-05-29', 1 ), array( '2015-06-02', 2 ) ); $output = array(); foreach ($array $val) { $month = date('m', strtotime($val[0])); if (isset($output[$month])) { $output[$month] += $val[1]; } else { $output[$month] = $val[1]; } } echo '<pre>'; print_r($output); /* array ( [05] => 2 [06] => 2 ) */ ?>
Comments
Post a Comment