내 앱에 메시징 시스템이 있고 사용자가 읽고 싶은 대화를 선택할 수있는 메뉴를 제공하는 가장 좋은 방법을 찾으려고 노력하고 있습니다. 왼쪽에 대화가있는 GroupMe 스타일 메뉴를 미러링하려고합니다. 이 대화 목록은 동적이므로 앱을 사용하는 동안 가장 최근 대화가 맨 위에 표시되고 업데이트됩니다.
Laravel 7과 Firebase를 사용하고 있습니다.
내 백엔드의 데이터는 다음과 같습니다.
Array
(
[-MC1kTfrrDHY3ZbidJ4Q] => Array
(
[from] => lSUfZ4sgEJd
[message] => test message four no more!
[startedAt] => 2020-07-12 11:21:10
[to] => CWgPqdn3YweN
)
[-MC09BsjtXP0izITsThf] => Array
(
[from] => CWgPqdn3YweN
[message] => test message three
[startedAt] => 2020-07-11 11:20:19
[subject] => -MC00BHCZlXUp25C5nlS
[to] => lSUfZ4sgEJd
)
[-MC1kAi1niswXtjY_h4s] => Array
(
[from] => CWgPqdn3YweN
[message] => test message two
[startedAt] => 2020-07-12 11:19:52
[to] => lSUfZ4sgEJd
)
[-MC1kOtfnIlAYtmJsD-a] => Array
(
[from] => CWgPqdn3YweN
[message] => test message one
[startedAt] => 2020-07-12 11:18:50
[to] => lSUfZ4sgEJd
)
[-MC1kOtfnIlAhgcufu] => Array
(
[from] => CWgPqdn3YweN
[message] => test message zero
[startedAt] => 2020-07-12 11:00:50
[to] => YLisXjk07w93
)
)
나는 발신자이든 수신자이든 상관없이 최신의 뚜렷한 대화를 얻고 싶습니다. 내가 원하는 최종 결과는 다음과 같습니다.
Array
(
[-MC1kTfrrDHY3ZbidJ4Q] => Array
(
[from] => lSUfZ4sgEJd
[message] => test message four no more!
[startedAt] => 2020-07-12 11:21:10
[to] => CWgPqdn3YweN
)
[-MC1kOtfnIlAhgcufu] => Array
(
[from] => CWgPqdn3YweN
[message] => test message zero
[startedAt] => 2020-07-12 11:00:50
[to] => YLisXjk07w93
)
)
이것에 대한 지침을 제공하는 사람이 있습니까? 나는 StackOverflow에 대한 응답과 함께 연관 배열의 배열에서 하나의 속성의 고유 값을 얻으 려고 시도했지만 $key
메시지의 ID 인 배열을 잃었습니다 .
이 솔루션을 살펴보십시오 : 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
)
);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다