如何按键分组条纹费用

时间:2018-03-04 21:44:47

标签: php multidimensional-array stripe-payments

我正在寻找创建捐赠网站,其中金额将按团队分组。我试图通过Stripe获取所有费用,然后将它们分组。 我怎样才能获得每个团队捐赠的总金额?

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 个答案:

答案 0 :(得分:0)

我找到了!

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) );
            }

        }
    }


}

}