根据识别数据分别删除重复的子数组,保留最后出现的重复项

Remove duplicate subarrays based on identifying data respectively retaining the last occurring duplicates

我从 api 查询中获得以下数据,我需要删除具有重复员工 ID 值的数据集并保留最后出现的数据集。

$holiday_array = [
    [
        'employee' => [
            'id' => 456062
        ],
        'reviewed_by' => [
            'id' => 260700
        ],
        'reason' => null,
        'type' => 'Holiday',
        'deducted' => '11.0',
        'id' => 11505539,
        'start_date' => '2021-03-19',
        'end_date' => '2021-04-02',
        'action' => 'request',
        'status]'=> 'approved',
        'created_at' => '2021-02-22T09:19:57+00:00',
        'updated_at' => '2021-02-23T13:28:41+00:00',
    ],
    [
        'employee' => [
            'id' => 522010
        ],
        'reviewed_by' => [
            'id' => 260760
        ],
        'reason' => null,
        'type' => 'Holiday',
        'deducted' => '2.0',
        'id' => 11730818,
        'start_date' => '2021-03-19',
        'end_date' => '2021-03-22',
        'action' => 'request',
        'status'=> 'approved',
        'created_at' => '2021-03-10T14:14:48+00:00',
        'updated_at' => '2021-03-15T08:04:36+00:00',
    ],
    [
        'employee' => [
            'id' => 638070
        ],
        'reviewed_by' => [
            'id' => 578193
        ],
        'reason' => null,
        'type' => 'Holiday',
        'deducted' => '1.0',
        'id' => 11861461,
        'start_date' => '2021-03-22',
        'action' => 'request',
        'status' => 'approved',
        'notes' => 'test',
        'created_at' => '2021-03-22T14:30:33+00:00',
        'updated_at' => '2021-03-22T14:31:39+00:00'
    ],
    [
        'employee' => [
            'id' => 638070
        ],
        'reviewed_by' => [
            'id' => 578193
        ],
        'reason' => null,
        'type' => 'Holiday',
        'deducted' => '1.0',
        'id' => 11861498,
        'start_date' => '2021-03-22',
        'action' => 'cancel',
        'status' => 'approved',
        'created_at' => '2021-03-22T14:31:55+00:00',
        'updated_at' => '2021-03-22T14:32:26+00:00'
    ],
    [
        'employee' => [
            'id' => 351779
        ],
        'reviewed_by' => [
            'id' => 578193
        ],
        'reason' => null,
        'type' => 'Holiday',
        'deducted' => '1.0',
        'id' => 11863071,
        'start_date' => '2021-03-22',
        'action' => 'request',
        'status' => 'approved',
        'notes' => 'Test',
        'created_at' => '2021-03-22T15:28:48+00:00',
        'updated_at' => '2021-03-23T14:41:13+00:00'
    ],
    [
        'employee' => [
            'id' => 638070
        ],
        'reviewed_by' => [
            'id' => 578193
        ],
        'reason' => null,
        'type' => 'Holiday',
        'deducted' => '1.0',
        'id' => 11864185,
        'start_date' => '2021-03-22',
        'action' => 'request',
        'status' => 'approved',
        'notes' => 'test',
        'created_at' => '2021-03-22T16:14:15+00:00',
        'updated_at' => '2021-03-22T16:41:18+00:00'
    ],
    [
        'employee' => [
            'id' => 638070
        ],
        'reviewed_by' => [
            'id' => 578193
        ],
        'reason' => null,
        'type' => 'Holiday',
        'deducted' => '1.0',
        'id' => 11877400,
        'start_date' => '2021-03-22',
        'action' => 'cancel',
        'status' => 'approved',
        'created_at' => '2021-03-23T14:24:54+00:00',
        'updated_at' => '2021-03-23T14:32:35+00:00'
    ],
    [
        'employee' => [
            'id' => 351779
        ],
        'reviewed_by' => [
            'id' => 578193
        ],
        'reason' => null,
        'type' => 'Holiday',
        'deducted' => '1.0',
        'id' => 11878419,
        'start_date' => '2021-03-22',
        'action' => 'cancel',
        'status' => 'approved',
        'created_at' => '2021-03-23T15:10:22+00:00'
    ],
    [
        'employee' => [
            'id' => 351779
        ],
        'reviewed_by' => [
            'id' => 578193
        ],
        'reason' => null,
        'type' => 'Holiday',
        'deducted' => '1.0',
        'id' => 11878445,
        'start_date' => '2021-03-22',
        'action' => 'cancel',
        'status' => 'approved',
        'created_at' => '2021-03-23T15:11:47+00:00'
    ],
    [
        'employee' => [
            'id' => 351779
        ],
        'reviewed_by' => [
            'id' => 578193
        ],
        'reason' => null,
        'type' => 'Holiday',
        'deducted' => '1.0',
        'id' => 11878450,
        'start_date' => '2021-03-22',
        'action' => 'cancel',
        'status' => 'approved',
        'created_at' => '2021-03-23T15:11:53+00:00'
    ]
]

10 组数据中只有 4 组属于唯一的员工 ID,因此我需要以下输出:

Array
(
    [0] => Array
        (
            [employee] => Array
                (
                    [id] => 456062
                )

            [reviewed_by] => Array
                (
                    [id] => 260700
                )

            [reason] => 
            [type] => Holiday
            [deducted] => 11.0
            [id] => 11505539
            [start_date] => 2021-03-19
            [end_date] => 2021-04-02
            [action] => request
            [status] => approved
            [created_at] => 2021-02-22T09:19:57+00:00
            [updated_at] => 2021-02-23T13:28:41+00:00
        )
[1] => Array
    (
        [employee] => Array
            (
                [id] => 522010
            )

        [reviewed_by] => Array
            (
                [id] => 260760
            )

        [reason] => 
        [type] => Holiday
        [deducted] => 2.0
        [id] => 11730818
        [start_date] => 2021-03-19
        [end_date] => 2021-03-22
        [action] => request
        [status] => approved
        [created_at] => 2021-03-10T14:14:48+00:00
        [updated_at] => 2021-03-15T08:04:36+00:00
    )
[6] => Array
    (
        [employee] => Array
            (
                [id] => 638070
            )

        [reviewed_by] => Array
            (
                [id] => 578193
            )

        [reason] => 
        [type] => Holiday
        [deducted] => 1.0
        [id] => 11877400
        [start_date] => 2021-03-22
        [action] => cancel
        [status] => approved
        [created_at] => 2021-03-23T14:24:54+00:00
        [updated_at] => 2021-03-23T14:32:35+00:00
    )
[9] => Array
    (
        [employee] => Array
            (
                [id] => 351779
            )

        [reviewed_by] => Array
            (
                [id] => 578193
            )

        [reason] => 
        [type] => Holiday
        [deducted] => 1.0
        [id] => 11878450
        [start_date] => 2021-03-22
        [action] => cancel
        [status] => approved
        [created_at] => 2021-03-23T15:11:53+00:00
    )

)

所有数组必须按键值 [employee][id] 排序, 如果不存在具有相同[employee][id]的重复数组,则只输出孤数组, 如果有例如 4 个相同的 (2,3,5,6),则输出最后一个数组 (6).

我写了这样的循环,但对我来说只推导了工人的id,我需要推导这些id进入的所有最后一个数组。

for($i = 0; $i < count($holiday_array); $i++) {
    $holiday_arrays[] = $holiday_array[$i]["employee];
    $array[] = array_unique($holiday_arrays[$i], SORT_REGULAR);
}
return $array;

因为php不允许在数组的任何级别上重复键,您可以滥用此规则并根据深度员工 ID 在结果数组上分配临时的第一级键。完成后只需重新索引结果数组。

我假设保留原始的一级密钥没有任何价值。

您的示例输入过于冗长,因此我已将其缩减为有意义的部分,以证明保留了最后出现的条目。

代码:(Demo)

$holiday_array = [
    ['employee' => ['id' => 456062], 'num' => 1],
    ['employee' => ['id' => 522010], 'num' => 1],
    ['employee' => ['id' => 638070], 'num' => 1],
    ['employee' => ['id' => 638070], 'num' => 2],
    ['employee' => ['id' => 351779], 'num' => 1],
    ['employee' => ['id' => 638070], 'num' => 3],
    ['employee' => ['id' => 638070], 'num' => 4],
    ['employee' => ['id' => 351779], 'num' => 2],
    ['employee' => ['id' => 351779], 'num' => 3],
    ['employee' => ['id' => 351779], 'num' => 4],
];

$result = [];
foreach ($holiday_array as $row) {
    $result[$row['employee']['id']] = $row;
}
var_export(array_values($result));

输出:

array (
  0 => 
  array (
    'employee' => 
    array (
      'id' => 456062,
    ),
    'num' => 1,
  ),
  1 => 
  array (
    'employee' => 
    array (
      'id' => 522010,
    ),
    'num' => 1,
  ),
  2 => 
  array (
    'employee' => 
    array (
      'id' => 638070,
    ),
    'num' => 4,
  ),
  3 => 
  array (
    'employee' => 
    array (
      'id' => 351779,
    ),
    'num' => 4,
  ),
)

感谢您的快速回复,但是我在数组中的值发生了变化,我以这个为例,所以每个查询我得到 returns 中间不同数量的数组,以及不同数量的数组相同的数组,有时没有相同的数组,所以不幸的是它不适合,我已经大致找到了解决方案,它看起来像这样:

$details = get_user_holiday_request_approved();
$reversed = array_reverse($details);
function unique_multidim_array($array, $key){
        $temp_array = array();
        $i = 0;
        $key_array = array();
        foreach($array as $val) {
            if (!in_array($val[$key], $key_array)) {
                $key_array[$i] = $val[$key];
                $temp_array[$i] = $val;
            }
            ++$i;
        }
        return $temp_array;
    };
    $details_new = unique_multidim_array($reversed,'employee');

但它给我带来了数组的含义 0、1、2、4,也就是说,它删除了所有重复值并输出第一个值,但我需要最后一个值,我将函数应用于我的数组:

array_reverse();