There is an array of "movements" of elements, about the same, only it can be many times more:
Array
(
[0] => Array
(
[id] => 90415
[oldDate] => 2024-08-27
[newDate] => 2024-08-28
)
[1] => Array
(
[id] => 90415
[oldDate] => 2024-08-28
[newDate] => 2024-08-27
)
[2] => Array
(
[id] => 90409
[oldDate] => 2024-08-21
[newDate] => 2024-08-22
)
[3] => Array
(
[id] => 90409
[oldDate] => 2024-08-22
[newDate] => 2024-08-23
)
)
I’m trying to make sure that all the intermediate movements of the element are deleted, only the initial and final ones remain, and the elements of the array are deleted, where the element is returned to its original position (0 and 1 elements of the array). In the end, the processed array should look like this:
Array
(
[0] => Array
(
[id] => 90409
[oldDate] => 2024-08-21
[newDate] => 2024-08-23
)
)
how can I do this so that an array of a large number of elements does not delay the script for a long time
2
Answers
Here is a solution that uses an intermediate
$data
associative array, which allows efficient lookups:Output:
Simply for comparison, here is the functional-style equivalent of Olivier’s (flawless) solution. Demo
It could also be written as: Demo