根据子值合并多维数组的子数组

问题描述 投票:0回答:2

我有一个这样的多维数组,我需要合并具有相同messageID值的子数组


$myarray = Array (

    [0] => Array
        (
            [messageId] => 5ACE9D8841
            [sender] => [email protected]
        )

    [1] => Array
        (
            [messageId] => 7EE67D8170
            [sender] => [email protected]
        )

    [2] => Array
        (
            [messageId] => 8095FD8836
            [sender] => [email protected]
        )

    [3] => Array
        (
            [messageId] => 7EE67D8170
            [dest] => [email protected]
            [status] => sent
            [date] => 2021-02-01 11:06:55
        )
);

Expected result , [1] 和 [3] 合并为 [1] 因为它们共享相同的 [messageId] :


Array
(
    [0] => Array
        (
            [messageId] => 5ACE9D8841
            [sender] => [email protected]
        )

    [1] => Array
        (
            [messageId] => 7EE67D8170
            [dest] => [email protected]
            [status] => sent
            [date] => 2021-02-01 11:06:55
            [sender] => [email protected]
        )

    [2] => Array
        (
            [messageId] => 8095FD8836
            [sender] => [email protected]
        )
)

我不介意键索引或顺序。

编辑:我尝试过 array_merge、array_merge_recursive 和许多其他方法。获得最佳结果


        foreach ($myarray as $sub_arr) {

            $result_arr = array_merge($result_arr, $sub_arr);

            $result_arr = array_unique($result_arr);


        } 

它有效但只返回最后一次迭代:

Array
        (
            [messageId] => 7EE67D8170
            [dest] => [email protected]
            [status] => sent
            [date] => 2021-02-01 11:06:55
            [sender] => [email protected]
        )

问候

php arrays multidimensional-array array-merge
2个回答
0
投票

尝试使用带有回调函数的

array_reduce
函数:

$result = array_values(array_reduce($myarray, function($rows, $item){
    if (array_key_exists('messageId', $item) && is_scalar($item['messageId'])) {
        $rows = array_replace_recursive($rows ?? [], [$item['messageId'] => $item]);
    }
    return $rows;
}));

print_r($result);

小提琴


0
投票

这可以通过将引用变量推入结果数组来在单个循环中完成。当您想生成一个输出大小/类型可能与输入数组不同的数组时,

array_reduce()
是一个合适的本机函数。

static
声明确保
$ref
数组在每次迭代之间不会被遗忘。通过将唯一的
&$ref
元素推入结果数组,您实际上不需要跟踪如何访问结果数组中行的位置——您只需要将数据推入引用变量,数据是完美地传递到结果数组中的正确位置。

代码:(演示

var_export(
    array_reduce(
        $myarray,
        function($result, $row) {
            static $ref;
            if (!isset($ref[$row['messageId']])) {
                $ref[$row['messageId']] = $row;
                $result[] = &$ref[$row['messageId']];
            } else {
                $ref[$row['messageId']] += $row;
            }
            return $result;
        }
    )
);
© www.soinside.com 2019 - 2024. All rights reserved.