PHP数组在单个数组中组合多个ID

时间:2015-08-20 10:13:32

标签: php

Array
(
    [0] => Array
        (
            [transaction_user_id] => 359691e27b23f8ef3f8e1c50315cd506
            [transaction_no] => 19500912050218
            [transaction_total_amount] => 589.00
            [transaction_date] => 1335932419
            [transaction_status] => cancelled
        )

    [1] => Array
        (
            [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d
            [transaction_no] => 36010512050819
            [transaction_total_amount] => 79.00
            [transaction_date] => 1336476696
            [transaction_status] => cancelled
        )

    [2] => Array
        (
            [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d
            [transaction_no] => 19020512050820
            [transaction_total_amount] => 299.00
            [transaction_date] => 1336476739
            [transaction_status] => cancelled
        )

    [3] => Array
        (
            [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d
            [transaction_no] => 27050512050821
            [transaction_total_amount] => 79.00
            [transaction_date] => 1336476927
            [transaction_status] => cancelled
        )

    [4] => Array
        (
            [transaction_user_id] => 8e9050a3646c98342b9ba079fba80982
            [transaction_no] => 12070512050822
            [transaction_total_amount] => 129.00
            [transaction_date] => 1336477032
            [transaction_status] => cancelled
        )

)

如果我想为类似的ID制作这样的数组     那我怎么能这样做呢?

[4] => Array
        (
            [transaction_user_id] => 8e9050a3646c98342b9ba079fba80982
            [transaction_no] => 12070512050822
            [transaction_total_amount] => array(
                 array('price'=>129.00),
                 array('price'=>79.00)
            )
            [transaction_date] => 1336477032
            [transaction_status] => cancelled
        );

我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

$myArr = array(
array
    ('transaction_user_id' => '359691e27b23f8ef3f8e1c50315cd506',
    'transaction_no' => '19500912050218',
    'transaction_total_amount' => '589.00',
    'transaction_date' => '1335932419',
    'transaction_status' => 'cancelled'
),
array
    ('transaction_user_id' => '359691e27b23f8ef3f8e1c50315cd506',
    'transaction_no' => '19500912050218',
    'transaction_total_amount' => '589.00',
    'transaction_date' => '1335932419',
    'transaction_status' => 'cancelled'
),
array
    ('transaction_user_id' => '359691e27b23f8rf3f8e1c50315cd506',
    'transaction_no' => '19500912050218',
    'transaction_total_amount' => '589.00',
    'transaction_date' => '1335932419',
    'transaction_status' => 'cancelled'
)
);

$newArr = array();
array_walk($myArr, "myfunc");

function myfunc(&$arr, $index) {
    global $newArr;
    $is_avail = false;
    $avail_index = 0;
    if (count($newArr) > 0) {
        foreach ($newArr as $key => $item) {
            if (in_array($arr['transaction_user_id'], $item)) {
                $is_avail = true;
                $avail_index = $key;
                break;
            }
        }
    }
    if ($is_avail) {
        array_push($newArr[$avail_index]['transaction_total_amount'], array("price" => $arr['transaction_total_amount']));
    } else {
        $newArr[] = $arr;
        $newArr[count($newArr) - 1]['transaction_total_amount'] = array(array("price" => $newArr[count($newArr) - 1]['transaction_total_amount']));
    }
}

print_r($newArr);

其中$myArr是您的数组。我已经从你的例子中创建了示例数组。我已经运行了这段代码并在我的最后工作正常。

答案 1 :(得分:0)

试试这个:

$old_data = array
(
    array ("transaction_user_id" => '359691e27b23f8ef3f8e1c50315cd506', "transaction_no" => '19500912050218', "transaction_total_amount" => '589.00', "transaction_date" => '1335932419', "transaction_status" => 'cancelled'),
    array ("transaction_user_id" => '9def02e6337b888d6dbe5617a172c18d', "transaction_no" => '36010512050819', "transaction_total_amount" => '79.00', "transaction_date" => '1336476696', "transaction_status" => 'cancelled'),
    array ("transaction_user_id" => '9def02e6337b888d6dbe5617a172c18d', "transaction_no" => '19020512050820', "transaction_total_amount" => '299.00', "transaction_date" => '1336476739', "transaction_status" => 'cancelled'),
    array ("transaction_user_id" => '9def02e6337b888d6dbe5617a172c18d', "transaction_no" => '27050512050821', "transaction_total_amount" => '79.00', "transaction_date" => '1336476927', "transaction_status" => 'cancelled'),
    array ("transaction_user_id" => '8e9050a3646c98342b9ba079fba80982', "transaction_no" => '12070512050822', "transaction_total_amount" => '129.00', "transaction_date" => '1336477032', "transaction_status" => 'cancelled')
);

方法1

$unique_data = array();
foreach ($old_data as $inner_array) {
    $unique_data[$inner_array['transaction_user_id']][] = $inner_array;
}

foreach ($unique_data as $index => $inner_array) {
    $unique_data[$index]['grand_total'] = 0;

    foreach ($inner_array as $value) {
        $unique_data[$index]['grand_total'] += $value['transaction_total_amount'];
    }
}

方法2

$unique_data = array();
foreach ($old_data as $inner_array) {
    $unique_data[$inner_array['transaction_user_id']]['transactions'][] = $inner_array;
}

foreach ($unique_data as $index => $inner_array) {
    $unique_data[$index]['grand_total'] = 0;

    foreach ($inner_array['transactions'] as $value) {
        $unique_data[$index]['grand_total'] += $value['transaction_total_amount'];
    }
}

输出以查看数组的结果:

echo "<pre>";
print_r($unique_data);
echo "</pre>";
exit();