Jump to content

[SOLVED] random shuffle array ???


nicob

Recommended Posts

Hello

 

I want to randomize the lines of a list.

 

example of the list:

 

line 1
line 2
line 3
...

 

My not working code  :'(

<?php

$file = file_get_contents('index.php');
$data = explode("\n",$file);

$newfile = shuffle($data);
$newfile = implode("\n",$newfile);

echo $newfile;

?>

 

Where does it go wrong??

 

 

Link to comment
https://forums.phpfreaks.com/topic/129931-solved-random-shuffle-array/
Share on other sites

shuffle returns bool so you have to do it like this.

 


<?php

$file = file_get_contents('index.php');
$data = explode("\n",$file);

if(shuffle($data)) {
    $newfile = implode("\n",$data);
    echo "SUCCESS !!! <br>";
    echo $newfile;
}
else echo "Fail !!! <br>";

?>
[/CODe]

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.