взгляните на это решение: https://3v4l.org/msUPQ
Я добавил несколько комментариев по коду на случай, если он не ясен:)
Работает, пока как предусмотрено $data
упорядочивается по последнему первому (в соответствии со спецификациями).
<?php
declare(strict_types=1);
$data = [
'-MC1kTfrrDHY3ZbidJ4Q' => [
'from' => 'lSUfZ4sgEJd',
'message' => 'test message four no more!',
'startedAt' => '2020-07-12 11:21:10',
'to' => 'CWgPqdn3YweN',
],
'-MC09BsjtXP0izITsThf' => [
'from' => 'CWgPqdn3YweN',
'message' => 'test message three',
'startedAt' => '2020-07-11 11:20:19',
'subject' => '-MC00BHCZlXUp25C5nlS',
'to' => 'lSUfZ4sgEJd',
],
'-MC1kAi1niswXtjY_h4s' => [
'from' => 'CWgPqdn3YweN',
'message' => 'test message two',
'startedAt' => '2020-07-12 11:19:52',
'to' => 'lSUfZ4sgEJd',
],
'-MC1kOtfnIlAYtmJsD-a' => [
'from' => 'CWgPqdn3YweN',
'message' => 'test message one',
'startedAt' => '2020-07-12 11:18:50',
'to' => 'lSUfZ4sgEJd',
],
'-MC1kOtfnIlAhgcufu' => [
'from' => 'CWgPqdn3YweN',
'message' => 'test message zero',
'startedAt' => '2020-07-12 11:00:50',
'to' => 'YLisXjk07w93',
],
];
function getLatestUniqueConversationMessagesByUser(array $message, string $user): array
{
$conversations = [];
$latestUniqueConversationMessages = [];
foreach ($message as $messageKey => $message) {
$participants = [$message['from'], $message['to']];
// Skip entries where $user is has not participated (not sure if needed)
if (!in_array($user, $participants)) { continue; }
// Make "to|from" same as "from|to"
sort($participants);
$conversationKey = join('|', $participants);
// Check if conversation has been handled already
if (!array_key_exists($conversationKey, $conversations)) {
$conversations[$conversationKey] = true; // Save as "handled"
$latestUniqueConversationMessages[$messageKey] = $message; // Save actual data to return
}
}
return $latestUniqueConversationMessages;
}
var_dump(getLatestUniqueConversationMessagesByUser($data, 'CWgPqdn3YweN'));
Вывод:
array(2) {
["-MC1kTfrrDHY3ZbidJ4Q"]=>
array(4) {
["from"]=>
string(11) "lSUfZ4sgEJd"
["message"]=>
string(26) "test message four no more!"
["startedAt"]=>
string(19) "2020-07-12 11:21:10"
["to"]=>
string(12) "CWgPqdn3YweN"
}
["-MC1kOtfnIlAhgcufu"]=>
array(4) {
["from"]=>
string(12) "CWgPqdn3YweN"
["message"]=>
string(17) "test message zero"
["startedAt"]=>
string(19) "2020-07-12 11:00:50"
["to"]=>
string(12) "YLisXjk07w93"
}
}
Альтернатива с использованием функций массива (не так читаемых):
$messageKeys =
array_keys(
array_unique(
array_map(
function ($message) {
$participants = [$message['from'], $message['to']];
sort($participants);
return join('|', $participants);
},
$data
)
)
);
var_dump(
array_filter(
$data,
function ($key) use ($messageKeys) {
return in_array($key, $messageKeys);
},
ARRAY_FILTER_USE_KEY
)
);