如何匹配数组键并在新数组中插入值?

时间:2014-02-04 05:30:55

标签: php arrays associative-array array-key

我有一个名为$transaction_count的数组,如下所示:

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

上面的数组是动态生成的,因此可以根据触发的查询更改数组键,即0,1,2,3。也就是说,数组也可以采用以下形式或任何其他形式:

Array
    (

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


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

现在我想根据条件设置以下四个变量值。

$success_transaction_count, $inprocess_transaction_count, $cancelled_transaction_count, $failed_transaction_count

例如,如果数组$transaction_count如下所示:

Array
        (

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

            )


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

因此,在上述情况下,值应设置如下:

$failed_transaction_count = 10

$cancelled_transaction_count = 25

和其他两个变量不应该设置,因为没有匹配键。

我应该如何正确设置变量的值?

2 个答案:

答案 0 :(得分:1)

如果我理解你想要什么,这可能会有效:

$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>";
}

输出:

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

请注意,我宁愿设置所有状态计数(即未明确计数的设置为0而不是未定义),但我想你有理由。

答案 1 :(得分:-1)

试试这个

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