0

I get the following data from an api query and I need to remove sets of data with duplicate employee id values and retain the last occurring dataset.

$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'
    ]
]

Only 4 of the 10 sets of data belong to unique employee ids, so I need the following output:

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
    )

)

All arrays must be sorted by key values [employee][id], If there are no duplicated arrays with the same [employee][id], then just output the solitary array, and if there are for example 4 identical (2,3,5,6), then output the last array (6).

I wrote such cycle, but to me deduces only id of workers, and I need to deduce all last arrays in which these id enter.

for($i = 0; $i < count($holiday_array); $i++) {
    $holiday_arrays[] = $holiday_array[$i]["employee];
    $array[] = array_unique($holiday_arrays[$i], SORT_REGULAR);
}
return $array;
mickmackusa
  • 43,625
  • 12
  • 83
  • 136

2 Answers2

0

Because php will not allow duplicate keys on any level of an array, you can abuse this rule and assign temporary first-level keys on the result array based on the deep employee id. When finished just re-index the result array.

I am assuming there is no value in retaining the original first level keys.

Your sample input was too verbose, so I have reduced it to its meaningful parts to demonstrate that the last occurring entries are retained.

Code: (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));

Output:

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,
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • thank you friend but you misunderstood me, see my answer above, I needed something that would work on this principle – Serhii Slobodian Mar 24 '21 at 18:23
  • @SerhiiSlobodian Here is proof that my technique works on your sample data. https://3v4l.org/gT00Z If this is not what you want, then your question should be closed as Unclear. – mickmackusa Mar 25 '21 at 01:16
  • 1
    sorry I didn't understand you, really your approach is correct, and it takes much less than a solution, today I will run it with an array that changes, and I think that the result will be as I need – Serhii Slobodian Mar 25 '21 at 06:56
  • you are amazing, everything works, sorry for not understanding you at first but it is possible to explain how the loop understands that it is necessary to deduce the last value of a key? – Serhii Slobodian Mar 25 '21 at 07:03
  • As you iterate, the data is unconditionally pushed into the result array. Every new data set will overwrite the earlier data set which has the same identifying value. Here is the simplest possible form of this technique: https://3v4l.org/pAN9K – mickmackusa Mar 25 '21 at 07:06
  • that is, even if there are 100 identical arrays, the cycle will change from 2 to 1, 3 to 2, and so on, and output the last value? now I understand your first answer, I apologize again, it's really true, thank you – Serhii Slobodian Mar 25 '21 at 07:10
  • Please mark this page resolved by accepting my answer. – mickmackusa Mar 25 '21 at 07:15
0

thanks for the quick response, but my values in the array change, I gave this as an example, and so each query I get returns a different number of arrays in the middle, and a different number of identical arrays, and sometimes no identical array, so unfortunately it doesn't fit, I've already roughly found a solution, it looks like this:

$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');

but it brings me arrays in meaning 0,1,2,4, that is, it removes all duplicate values and outputs the first ones, but I need the last ones, I applied the function to my array:

array_reverse();