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?
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'];
}
}