3v4l.org

run code in 300+ PHP versions simultaneously
<?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'));
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
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" } }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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" } }

preferences:
169.78 ms | 403 KiB | 183 Q