0

i have an array like

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 ) 
)

i need to display this msg priority wise. The condition is msg will be changed on each page load. I have used shuffle() but it's not proper.

Is it possible to change the sequence of the array ??

duncan
  • 31,401
  • 13
  • 78
  • 99
codeBloger
  • 217
  • 2
  • 15

2 Answers2

1

finally what i have done is :

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 ) 
)

After that suffle() . .

codeBloger
  • 217
  • 2
  • 15
0

Store the messages in a/the session, display one on each load:

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'];

To rotate the array (feedback to comment):

$messages[] = array_shift($messages);
deceze
  • 510,633
  • 85
  • 743
  • 889
  • i have tried it ??? :( .. its not the solution fr MVC and ajax base loading i have also tried to create the session on index page but when the ajes load its clared the session – codeBloger Feb 24 '16 at 16:27
  • Huh? Why is it not a solution "for MVC", and how does AJAX factor into this? – deceze Feb 24 '16 at 16:28
  • just give the answer yes or no .. is their any method to shuffle the array sequentially ??? like this 0 1 2 3 4 ---> 1 2 3 4 0 ---> 2 3 4 0 1 – codeBloger Feb 24 '16 at 16:33
  • That's not "shuffling sequentially", that's called *rotating*. I've added how to do that to the answer. – deceze Feb 24 '16 at 16:35
  • There's no big difference between rotating the array and doing what I'm suggesting, just that rotating uses more session storage (assuming you'd store that in the session). – deceze Feb 24 '16 at 16:36