亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據嵌套鍵值對獲取不同的關聯數組

根據嵌套鍵值對獲取不同的關聯數組

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 關注
  • 153 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號