我从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]排序,如果没有重复的数组具有相同的[雇员][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在结果数组上分配临时的一级键。完成后,只需重新索引结果数组。
我认为保留最初的一级密钥没有任何价值。
您的示例输入太过冗长,所以我将其简化为有意义的部分,以证明保留了最后出现的条目。
代码:(演示(
$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,
),
)
感谢您的快速响应,但我在数组中的值发生了变化,我举了一个例子,所以我得到的每个查询都会返回中间不同数量的数组,以及不同数量的相同数组,有时甚至没有相同的数组,所以不幸的是,它不适合,我已经大致找到了一个解决方案,看起来如下:
$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();