3v4l.org

run code in 500+ 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'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/msUPQ
function name:  (null)
number of ops:  9
compiled vars:  !0 = $data
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    5     0  E >   ASSIGN                                                       !0, <array>
   64     1        INIT_FCALL                                                   'var_dump'
          2        INIT_FCALL                                                   'getlatestuniqueconversationmessagesbyuser'
          3        SEND_VAR                                                     !0
          4        SEND_VAL                                                     'CWgPqdn3YweN'
          5        DO_FCALL                                          0  $2      
          6        SEND_VAR                                                     $2
          7        DO_ICALL                                                     
          8      > RETURN                                                       1

Function getlatestuniqueconversationmessagesbyuser:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 32
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 32
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 31
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 31
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/msUPQ
function name:  getLatestUniqueConversationMessagesByUser
number of ops:  37
compiled vars:  !0 = $message, !1 = $user, !2 = $conversations, !3 = $latestUniqueConversationMessages, !4 = $messageKey, !5 = $participants, !6 = $conversationKey
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   39     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   41     2        ASSIGN                                                       !2, <array>
   42     3        ASSIGN                                                       !3, <array>
   44     4      > FE_RESET_R                                           $9      !0, ->32
          5    > > FE_FETCH_R                                           ~10     $9, !0, ->32
          6    >   ASSIGN                                                       !4, ~10
   45     7        FETCH_DIM_R                                          ~12     !0, 'from'
          8        INIT_ARRAY                                           ~13     ~12
          9        FETCH_DIM_R                                          ~14     !0, 'to'
         10        ADD_ARRAY_ELEMENT                                    ~13     ~14
         11        ASSIGN                                                       !5, ~13
   48    12        FRAMELESS_ICALL_2                in_array            ~16     !1, !5
         13        BOOL_NOT                                             ~17     ~16
         14      > JMPZ                                                         ~17, ->16
         15    > > JMP                                                          ->5
   51    16    >   INIT_FCALL                                                   'sort'
         17        SEND_REF                                                     !5
         18        DO_ICALL                                                     
   52    19        INIT_FCALL                                                   'join'
         20        SEND_VAL                                                     '%7C'
         21        SEND_VAR                                                     !5
         22        DO_ICALL                                             $19     
         23        ASSIGN                                                       !6, $19
   55    24        ARRAY_KEY_EXISTS                                     ~21     !6, !2
         25        BOOL_NOT                                             ~22     ~21
         26      > JMPZ                                                         ~22, ->31
   56    27    >   ASSIGN_DIM                                                   !2, !6
         28        OP_DATA                                                      <true>
   57    29        ASSIGN_DIM                                                   !3, !4
         30        OP_DATA                                                      !0
   44    31    > > JMP                                                          ->5
         32    >   FE_FREE                                                      $9
   61    33        VERIFY_RETURN_TYPE                                           !3
         34      > RETURN                                                       !3
   62    35*       VERIFY_RETURN_TYPE                                           
         36*     > RETURN                                                       null

End of function getlatestuniqueconversationmessagesbyuser

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
167.94 ms | 2244 KiB | 17 Q