-2

i have an array example:

Array
(
[0] => 5c832a3fec3a6
[1] => 5c832a3fdbe90
[2] => 5c832a3fc6335
[3] => 5c832a3fb080d
[4] => 5c832a3f89d5b
)

i want to result like this when i refersh my page.

first time refersh

Array
(
[0] => 5c832a3fc6335
[1] => 5c832a3f89d5b
[2] => 5c832a3fec3a6
[3] => 5c832a3fb080d
[4] => 5c832a3fdbe90
)

in second time refresh

Array
(
[0] => 5c832a3fc6335
[1] => 5c832a3fb080d
[2] => 5c832a3f89d5b
[3] => 5c832a3fdbe90
[4] => 5c832a3fec3a6
)

it means every time new random array in result.

D.priye
  • 32
  • 7
  • 1
    ok ... so at least make an attempt. –  Mar 09 '19 at 03:57
  • 2
    take a look at [`shuffle`](http://php.net/manual/en/function.shuffle.php) – Nick Mar 09 '19 at 03:57
  • I had tried "shuffle" but did not get the result. – D.priye Mar 09 '19 at 04:00
  • 2
    Well, show us what you tried, tell us what was wrong with the results that you got and what the result you expected was, and then we might be able to help you. At present there is not enough information in your question to be able to give you an answer. – Nick Mar 09 '19 at 04:06
  • Your question seems Unclear. Why is `shuffle()` not an ideal solution? – mickmackusa Mar 09 '19 at 04:11
  • If you can improve your question and clarify why `shuffle()` will not do, your question can be reopened. – mickmackusa Mar 09 '19 at 04:17

1 Answers1

-1

As an alternative to shuffle(), you can also use rand():

$array = [
0 => "5c832a3fec3a6",
1 => "5c832a3fdbe90",
2 => "5c832a3fc6335",
3 => "5c832a3fb080d",
4 => "5c832a3f89d5b",
];

while(!empty($array)){
    $key = rand(0,4);
    if(key_exists($key, $array)){
        $new_order[] = $array[$key];
    }
    unset($array[$key]);
}

var_dump($new_order);

Caveat

This is a costly exercise, so while it's relatively harmless for small arrays like this, I would probably stick to shuffle().

dearsina
  • 4,774
  • 2
  • 28
  • 34
  • While I gave you an answer because I sort of figured out what you wanted to achieve, it's better form for the OP to try something, and if it doesn't work, share what they tried and ask people here to help them figure out where they went wrong. Otherwise StackOverflow just becomes a place where people ask others to write bits of code for them. – dearsina Mar 09 '19 at 05:19