如何在php中的一列中仅在一行和相同日期添加一天的销售?

如何在php中的一列中仅在一行和相同日期添加一天的销售?

问题描述:

i have a array result like the below. it show every payment on each day but i want to add the all the payment in of a day in only one row and the date in one col how can i do that ? thanks

Array ( [cols] => 2017-09-10 [rows] => 1311.45 ) [1] => Array ( [cols] => 2017-09-10 [rows] => 1311.45 ) [2] => Array ( [cols] => 2017-09-10 [rows] => 175000.00 )

my code look like this

 foreach($Todaysales as $d)

 {

     $data[] = ['cols' => date("Y-m-d", strtotime($d['sale_time'])),'rows' => $d['payment_amount']];

 }

My desired output is

[[cols]=> 2017-09-10 [rows]=> sum(2017-09-10's sales)]

edited code

 $data=Array
(
    [2017-09-10] => 178934.35
    [2017-09-09] => 700000
    [2017-09-07] => 194432.25
    [2017-09-06] => 183252.9
    [2017-09-03] => 1311.45
    [2017-09-02] => 1186.55
    [2017-08-30] => 204660.3
    [2017-08-29] => 290486.45
    [2017-08-28] => 2400
    [2017-08-27] => 600.00
    [2017-08-26] => 840.00
    [2017-08-16] => 600.00
)

to this format after json encoding

[{"cols":"2017-09-02 ","rows":"1186.55"},{"cols":"2017-09-03","rows":"1311.45"}

我有一个如下所示的数组结果。 它显示每天的每笔付款,但我想在一天中添加所有付款,只在一行中,并且在一个col中的日期我怎么能这样做? 谢谢 p>

数组([cols] => 2017-09-10 [rows] => 1311.45)[1] => 数组([cols] => 2017-09-10 [行] => 1311.45)[2] => 数组([cols] => 2017-09-10 [行] => 175000.00) code> p>

我的代码看起来像这样 p> foreach($ Todaysales as $ d) { $ data [] = ['cols'=> date(“Y-m-d”,strtotime($ d ['sale_time'])),'rows'=> $ d ['payment_amount']]; } code> pre>

我想要的输出是 p>

  [  [COLS] =>  2017-09-10 [rows] =>  sum(2017-09-10销售)] 
  code>  pre> 
 
 

已编辑的代码 h1>
  $ data = Array 
(  
 [2017-09-10] => 178934.35 
 [2017-09-09] => 700000 
 [2017-09-07] => 194432.25 
 [2017-09-06] =&gt  ; 183252.9 
 [2017-09-03] => 1311.45 
 [2017-09-02] => 1186.55 
 [2017-08-30] => 204660.3 
 [2017-08-29]  => 290486.45 
 [2017-08-28] => 2400 
 [2017-08-27] => 600.00 
 [2017-08-26] => 840.00 
 [2017-08-  16] => 600.00 
)
  code>  pre> 
 
 

在json编码后使用此格式 p>

  [{“cols  “:”2017-09-02“,”rows“:”1186.55“},{”cols“:”2017-09-03“,”rows“:”1311.45“} 
  code>  pre>  
  div>

Try below code you will get date as key and sum of sales amount as value

foreach($Todaysales as $d)

 {

     if(isset($data[$d['sale_time']]))
         $data[$d['sale_time']] += $d['payment_amount'];
     else
         $data[$d['sale_time']] = $d['payment_amount'];

 }
 print_r($data);

DEMO

EDIT If you want same output as you specified. You can try following

$data = array();
foreach($Todaysales as $d)
{
     $match_key =false;
     $match_key = array_search($d['sale_time'],array_column($data, "cols"));

     if($match_key > 0 || $match_key === 0)
        $data[$match_key]["rows"] = $data[$match_key]["rows"] + $d['payment_amount'];
     else
         $data[] = array("cols"=>$d['sale_time'],"rows"=>$d['payment_amount']);

}
print_r($data);

DEMO

EDIT as per your comment you need it for google chart try below answer:

foreach($Todaysales as $d)

 {

     if(isset($data[$d['sale_time']]))
         $data[$d['sale_time']] += $d['payment_amount'];
     else
         $data[$d['sale_time']] = $d['payment_amount'];

 }

 $new_data["cols"] ="'".implode("','",array_keys($data))."'";
 $new_data["rows"] =implode(",",array_values($data));
 print_r($new_data);

DEMO

To modify the previous given array you can do so:

$Todaysales = [
     ['cols' => '2017-09-10', 'rows' => 1311.45],
     ['cols' => '2017-09-10', 'rows' => 1311.45],
     ['cols' => '2017-09-10', 'rows' => 175000.00]
];

foreach($Todaysales as $d)
 {
     $data[$d['cols']][] = $d['rows'];
 }

print_r($data);

will return:

Array
(
    [2017-09-10] => Array
        (
            [0] => 1311.45
            [1] => 1311.45
            [2] => 175000
        )

)

But it seems that you also generate the first array yourself, if so you should give us the code that generates $Todaysales if to change it at the origin.

[EDIT] after question edit

$Todaysales = [
     ['cols' => '2017-09-10', 'rows' => 1311.45],
     ['cols' => '2017-09-10', 'rows' => 1311.45],
     ['cols' => '2017-09-10', 'rows' => 175000.00]
];

$today_sales = ['cols' => date('Y-m-d'), 'rows' => 0];

foreach($Todaysales as $d)
 {
    $today_sales['rows'] += $d['rows'];
 }
print_r($today_sales);

returns

Array
(
    [cols] => 2017-09-10
    [rows] => 177622.9
)

$arr=[
    Array ( 'cols' => '2017-09-10', 'rows' => 1311.45 ), 
    Array ( 'cols' => '2017-09-10', 'rows' => 1311.45 ),
    Array ( 'cols'=> '2017-09-10', 'rows' => 175000.00 )
];
$r['cols']=$arr[0]['cols'];
$r['rows']=0;
for($i=0;$i<count($arr);++$i){
    $r['rows']+=$arr[$i]['rows'];
}
print_r($r);