douxu0550 2018-03-04 21:44
浏览 34
已采纳

如何按键分组条纹费用

I'm looking to create a Donation Website where the amounts would be grouped by team. I'm trying to get all charges via Stripe and then group them. How could I get the total amount donated for each team ?

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}


function TheMainFunction(){
    \Stripe\Stripe::setApiKey("KEY");
    $charges = \Stripe\Charge::all();


    $teams = array();
    foreach($charges->data as $charge) if(isset($charge->metadata['team']) ){

        if( !in_array_r($charge->metadata['team'], $teams ) ){
            array_push($teams, array( 'team'=> $charge->metadata['team'], 'amount' => $charge->amount) );
        }
        else{
            foreach( $teams as $k => $v ){
                if( $v['team'] == $charge->metadata['team']){
                    // The action should be done here
                }

            }
        }


    }
}
  • 写回答

1条回答 默认 最新

  • duanqin2026 2018-03-05 16:21
    关注

    I found how!

    function TheMainFunction(){
    \Stripe\Stripe::setApiKey("KEY");
    $charges = \Stripe\Charge::all();
    
    
    $teams = array();
    foreach($charges->data as $charge) if(isset($charge->metadata['team']) ){
    
        if( !in_array_r($charge->metadata['team'], $teams ) ){
            array_push($teams, array( 'team'=> $charge->metadata['team'], 'amount' => $charge->amount) );
        }
        else{
            foreach( $teams as $k => $v ){
                if( $v['team'] == $charge->metadata['team']){
                    $oldvalue = $v['amount'];
                    $newvalue = $oldvalue+$charge->amount;
                    unset($teams[$k]);
                    array_push($teams, array( 'team'=> $charge->metadata['team'], 'amount' => $newvalue) );
                }
    
            }
        }
    
    
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FOR循环语句显示查询超过300S错误怎么办
  • ¥15 数电设计题 没有设计思路 不知道用什么芯片进行设计 求提供设计思路
  • ¥60 设计一种优化算法结合案例给出智能仓储四向穿梭车的调度计划
  • ¥15 Errno2:No such file or directory,在当前文件确实没有该图片,怎么解决?
  • ¥15 博世摄像头数据存储的问题(iscsi)
  • ¥15 如何实现对学生籍贯信息管理系统的选择排序
  • ¥15 git clone报错
  • ¥15 3d-slicer超声造影动态图像导入报错
  • ¥15 化工过程分析与合成问题求解决
  • ¥15 化工过程分析与合成问题解答