hellonoko Posted March 2, 2009 Share Posted March 2, 2009 I have a script that creates a xspf playlist from a directory of files. However when I try to make the playlist random by using: shuffle(); instead of arsort(); the script write 0 1 2 3 etc to the XML file rather than the file names as arsort(); does. The line in question is 59: shuffle($arr); Any ideas? <?php //require_once("/flashupload2/config.php"); //////////////////////////////////////////////// /////////////////////////////////////////////// $username = "sharingi_music"; $password = "music"; $hostname = "localhost"; $db_connect = mysql_connect( $hostname , $username, $password) or die ("Unable to connect to database"); mysql_select_db( "sharingi_music" , $db_connect); ///////////////////////////////////////////////// //////////////////////////////////////////////// // create doctype $dom = new DOMDocument("1.0"); // display document in browser as plain text // for readability purposes //header("Content-Type: text/plain"); // create root element $root = $dom->createElement("playlist"); $dom->appendChild($root); // create attribute node $version = $dom->createAttribute("version"); $root->appendChild($version); // create attribute value node $versionValue = $dom->createTextNode("1.0"); $version->appendChild($versionValue); // create attribute node $xmnls = $dom->createAttribute("xmlns"); $root->appendChild($xmnls); // create attribute value node $xmnlsValue = $dom->createTextNode("http://xspf.org/ns/0/"); $xmnls->appendChild($xmnlsValue); // create track element $trackList = $dom->createElement("trackList"); $root->appendChild($trackList); $dir = '/home2/sharingi/public_html/REPOSITORY/'; foreach (glob( $dir.'*.*') as $filename) { $arr[$filename] = filemtime($filename); } shuffle($arr); foreach($arr as $key => $value) { if ( $key !== "/home2/sharingi/public_html/REPOSITORY/filelist3.php" && $key !== "/home2/sharingi/public_html/REPOSITORY/index.php" ) { $key = str_replace( "/home2/sharingi/public_html/REPOSITORY/" , "" , $key); //echo $key; //echo '<br>'; // create track element $track = $dom->createElement("track"); $trackList->appendChild($track); // create child element of track called location. $location = $dom->createElement("location"); $track->appendChild($location); //create location link $link = $dom->createTextNode("http://www.site.com/REPOSITORY/". $key); $location->appendChild($link); // create child element of track called title. $title = $dom->createElement("title"); $track->appendChild($title); ///get the song name// $result = mysql_query("SELECT SONG FROM songs WHERE HIDDENNAME = '$key'"); while ($row = mysql_fetch_row($result)) { $songname = $row[0]; } //create title data $songTitle = $dom->createTextNode("$songname"); $title->appendChild($songTitle); } } // save and display tree $dom->save("playlist.xspf"); mysql_close ( $db_connect); ?> <object type="application/x-shockwave-flash" width="400" height="170" data="http://www.site.com/fullplayer/xspf_player.swf?playlist_url=http://www.site.com/fullplayer/playlist.xspf"> <param name="movie" value="http://www.site.com.com/fullplayer/xspf_player.swf?playlist_url=http://www.site.com/fullplayer/playlist.xspf" /> </object> Quote Link to comment https://forums.phpfreaks.com/topic/147579-using-shuffle-to-randomize-an-array/ Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 Shuffle restructures the array/rebuilds it. array_rand look into using that, as it leaves your array in-tact but will just give you a random order of keys. $arrayKeys = array_rand($array, count($array)); I am not sure if that will return duplicate keys, but give that a try and see if that works for you. Quote Link to comment https://forums.phpfreaks.com/topic/147579-using-shuffle-to-randomize-an-array/#findComment-774733 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.