php - array grouping or sorting is not working -


here array.

 array(         [0] => array             (                 [amt] => 50.00                 [transaction_date] => 2015-07-18             )           [1] => array             (                 [amt] => 0.00                 [transaction_date] => 2015-07-17             )          [2] => array             (                 [amt] => 60.00                 [transaction_date] => 2015-07-18             )     ) 

i trying sort or grouping array according date.transaction date should 1 means no duplicate entry of transaction in array.

i want new array follows

array(             [0] => array                 (                     [amt] => 110.00                     [transaction_date] => 2015-07-18                 )              [1] => array                 (                     [amt] => 0.00                     [transaction_date] => 2015-07-17                 )                      ) 

2015-07-18 date appears 2 times in array 2 different amount amount should added per repetition. amount should 110

$temp=0;         $ctr=0;                     for($i=0;$i<count($new_data);$i++)         {             $temp=$new_data[$i]['transaction_date'];              for($j=0;$j<=$i;$j++)             {                 if($temp==$new_data[$j]['transaction_date'])                 {                                      $newarray[][$new_data[$j]['transaction_date']]=++$ctr;                  }              }             $ctr=0;         }                    print_r($newarray); 

try using foreach as

$result = array(); foreach ($output $key => $value) {     $hash = $value['transaction_date'];     if (isset($result[$hash])) {         $result[$hash]['amt'] += $value['amt'];         $result[$hash]['transaction_date'] = $value['transaction_date'];     } else {         $result[$hash]['amt'] = $value['amt'];         $result[$hash]['transaction_date'] = $value['transaction_date'];     } } print_r(array_values($result)); 

demo


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -