根据优先级显示消息

时间:2016-02-24 16:05:15

标签: php

我有一个像

这样的数组
Array ( [0] => Array ( [msg] => xyx 
                       [social] =>xxx 
                       [priority] => 1 ) 
        [1] => Array ( [msg] => vvv 
                       [social] => 
                       [priority] => 2 ) 
        [2] => Array ( [msg] => hhh 
                       [social] => 
                       [priority] => 2 ) 
        [3] => Array ( [msg] => rrr 
                       [social] => jws 
                       [priority] => 2 ) 
        [4] => Array ( [msg] => hhhh. 
                       [social] => fff 
                       [priority] => 3 ) 
)

我需要明智地显示这个msg优先级。条件是msg将在每次页面加载时更改。我使用了shuffle(),但它不合适。

是否可以更改数组的序列?

2 个答案:

答案 0 :(得分:0)

将消息存储在/会话中,在每个负载上显示一个:

session_start();

if (empty($_SESSION['messages'])) {
    $_SESSION['messages'] = $messages;

    // sort by priority if necessary:
    // usort($_SESSION['messages'], function ($a, $b) { return $a['priority'] - $b['priority']; });
}

$message = array_shift($_SESSION['messages']);
echo $message['msg'];

旋转数组(反馈给评论):

$messages[] = array_shift($messages);

答案 1 :(得分:0)

最后我所做的是:

Array ( [0] => Array ( [msg] => xyx 
                       [social] =>xxx 
                       [priority] => 1 ) 
[0] => Array ( [msg] => xyx 
                       [social] =>xxx 
                       [priority] => 1 ) 
[0] => Array ( [msg] => xyx 
                       [social] =>xxx 
                       [priority] => 1 ) 
[0] => Array ( [msg] => xyx 
                       [social] =>xxx 
                       [priority] => 1 ) 
[0] => Array ( [msg] => xyx 
                       [social] =>xxx 
                       [priority] => 1 ) 
        [1] => Array ( [msg] => vvv 
                       [social] => 
                       [priority] => 2 ) 
        [2] => Array ( [msg] => hhh 
                       [social] => 
                       [priority] => 2 ) 
        [3] => Array ( [msg] => rrr 
                       [social] => jws 
                       [priority] => 2 ) 
        [4] => Array ( [msg] => hhhh. 
                       [social] => fff 
                       [priority] => 3 ) 
)

之后的那个()。 。