PHP组数组结果按值汇总

时间:2018-09-19 17:39:46

标签: php

我在PHP中有一个数组,我想按一个键=>值对([recordRow] =>?)将结果分组在一起。最有效的方法是什么。此数组中可能包含成千上万个元素...这是我的数组,谢谢!

Array
(
[0] => Array
    (
        [id] => 71060
        [user_id] => 266
        [import_id] => 45
        [recordValue] => 121212121
        [recordColumn] => 4
        [recordRow] => 1
        [recordMerged] => 
        [created] => 2018-09-19 05:01:24
    )

[1] => Array
    (
        [id] => 71061
        [user_id] => 266
        [import_id] => 45
        [recordValue] => fake@email.com
        [recordColumn] => 5
        [recordRow] => 1
        [recordMerged] => 
        [created] => 2018-09-19 05:01:24
    )

[2] => Array
    (
        [id] => 71089
        [user_id] => 266
        [import_id] => 45
        [recordValue] => 2323232323
        [recordColumn] => 4
        [recordRow] => 2
        [recordMerged] => 
        [created] => 2018-09-19 05:01:24
    )

[3] => Array
    (
        [id] => 71090
        [user_id] => 266
        [import_id] => 45
        [recordValue] => fake2@email.com
        [recordColumn] => 5
        [recordRow] => 2
        [recordMerged] => 
        [created] => 2018-09-19 05:01:24
    )

[4] => Array
    (
        [id] => 71118
        [user_id] => 266
        [import_id] => 45
        [recordValue] => 3434343434
        [recordColumn] => 4
        [recordRow] => 3
        [recordMerged] => 
        [created] => 2018-09-19 05:01:24
    )

[5] => Array
    (
        [id] => 71119
        [user_id] => 266
        [import_id] => 45
        [recordValue] => fake3@email.com
        [recordColumn] => 5
        [recordRow] => 3
        [recordMerged] => 
        [created] => 2018-09-19 05:01:24
    )
)

最后,这是我希望完成的工作:

Array
(
[0] => (
    Array
        [0](
            [id] => 71060
            [user_id] => 266
            [import_id] => 45
            [recordValue] => 121212121
            [recordColumn] => 4
            [recordRow] => 1
            [recordMerged] =>
            [created] => 2018-09-19 05:01:24
        )

        [1] => Array
        (
            [id] => 71061
            [user_id] => 266
            [import_id] => 45
            [recordValue] => fake@email.com
            [recordColumn] => 5
            [recordRow] => 1
            [recordMerged] =>
            [created] => 2018-09-19 05:01:24
        )
)
[1] => (
    [0] => Array
        (
            [id] => 71089
            [user_id] => 266
            [import_id] => 45
            [recordValue] => 2323232323
            [recordColumn] => 4
            [recordRow] => 2
            [recordMerged] =>
            [created] => 2018-09-19 05:01:24
        )

    [1] => Array
        (
            [id] => 71090
            [user_id] => 266
            [import_id] => 45
            [recordValue] => fake2@email.com
            [recordColumn] => 5
            [recordRow] => 2
            [recordMerged] =>
            [created] => 2018-09-19 05:01:24
        )
)
[2] => (
    [0] => Array
    (
        [id] => 71118
        [user_id] => 266
        [import_id] => 45
        [recordValue] => 3434343434
        [recordColumn] => 4
        [recordRow] => 3
        [recordMerged] =>
        [created] => 2018-09-19 05:01:24
    )

    [1] => Array
        (
            [id] => 71119
            [user_id] => 266
            [import_id] => 45
            [recordValue] => fake3@email.com
            [recordColumn] => 5
            [recordRow] => 3
            [recordMerged] =>
            [created] => 2018-09-19 05:01:24
        )
    )
)    

我已经尝试过:

$merged = array();
                        foreach ($records as $item)
                        {
                            $row = $item['recordRow'];
                            if (!isset($merged[$row]))
                            {
                                $merged[$row] = array();
                            }
                            $merged[$row] = array_merge($merged[$row], $item);
                        }

1 个答案:

答案 0 :(得分:1)

如果您不关心键(例如,键将与recordRow相同),并且您的结果集很小,则此代码将对您有效:

$output = [];
foreach($items as $item) {
    $output[$item['recordRow']][] = $item;
}

unset($items);
var_dump($output);