Question

I've an array called $transaction_count as follows:

Array
(
    [0] => Array
        (
            [transaction_status] => success
            [StatusCount] => 25
        )

    [1] => Array
        (
            [transaction_status] => inprocess
            [StatusCount] => 5
        )

    [2] => Array
        (
            [transaction_status] => cancelled
            [StatusCount] => 66
        )
    [3] => Array
        (
            [transaction_status] => fail
            [StatusCount] => 0
        )    
)

The above array gets generated dynamically so the array keys i.e.0,1,2,3 can be changed based on a query fired. That is the array could take following form or any other form too:

Array
    (

        [0] => Array
            (
                [transaction_status] => inprocess
                [StatusCount] => 5
            )


        [1] => Array
            (
                [transaction_status] => fail
                [StatusCount] => 0
            )    
    )

Now I want to set the following four variable values based on a condition.

$success_transaction_count, $inprocess_transaction_count, $cancelled_transaction_count, $failed_transaction_count

For example if the array $transaction_count is as below:

Array
        (

        [0] => Array
            (
                [transaction_status] => fail
                [StatusCount] => 10

            )


        [1] => Array
            (
               [transaction_status] => cancelled
               [StatusCount] => 25
            )    
    )

So in above case the values should get set as follows:

$failed_transaction_count = 10

$cancelled_transaction_count = 25

and other two variables should not get set as there are no matcing keys are present.

How should I set the values of variables properly?

Was it helpful?

Solution 2

try this

$success_transaction_count=0;
$inprocess_transaction_count=0;
$cancelled_transaction_count=0;
$failed_transaction_count=0;

foreach($$transaction_count as $arr)
{
    if($arr['transaction_status']=='success')
    {
        $success_transaction_count += $arr['StatusCount'];
    }
    else if($arr['transaction_status']=='inprocess')
    {
        $inprocess_transaction_count += $arr['StatusCount'];
    }
    else if($arr['transaction_status']=='cancelled')
    {
        $cancelled_transaction_count += $arr['StatusCount'];
    }
    else if($arr['transaction_status']=='fail')
    {
        $failed_transaction_count += $arr['StatusCount'];
    }
}

OTHER TIPS

If I understood what you want correctly, this might work:

$transaction_count = array(
    array ("transaction_status" => "failed", "StatusCount" => 10),
    array ("transaction_status" => "cancelled", "StatusCount" => 25));

function set_counts ($input)
{
    foreach ($input as $pair)
    {
        // get back count for a given type
        $type = $pair["transaction_status"];
        $count = $pair["StatusCount"];

        // set the corresponding variable
        $var_name = $type."_transaction_count";
        global $$var_name;
        $$var_name = $count;
    }
}

set_counts ($transaction_count);

foreach (array("success", "inprocess",  "cancelled",  "failed") as $type)
{
    $var_name = $type."_transaction_count";
    $value = isset($$var_name) ? $$var_name : "not set";
    echo "$var_name: $value<br>";
}

output:

success_transaction_count: not set
inprocess_transaction_count: not set
cancelled_transaction_count: 25
failed_transaction_count: 10

Note that I would rather have all status counts set (i.e. the ones not explicitely counted set to 0 instead of left undefined), but I suppose you have your reasons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top