为了账号安全,请及时绑定邮箱和手机立即绑定

根据嵌套键值对获取不同的关联数组

根据嵌套键值对获取不同的关联数组

PHP
冉冉说 2023-07-30 11:16:22
我的应用程序中有一个消息系统,我正在绞尽脑汁地试图找出为用户提供一个菜单来选择他们想要阅读的对话的最佳方法。我正在尝试镜像 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        ))有人介意提供这方面的指导吗?
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

看看这个解决方案: 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

    )

);


查看完整回答
反对 回复 2023-07-30
  • 1 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信