rondog Posted January 10, 2009 Share Posted January 10, 2009 I am generating an xml file via php. I have an array with xml nodes and would like to pick at random a node that hasn't already been chosen. Here is my php right now: <?php $items = array("<photo url=\"images/banner/image1.jpg\" caption=\"Redundancy\" click=\"\" />\n", "<photo url=\"images/banner/image2.jpg\" caption=\"Responsible\" click=\"\" />\n", "<photo url=\"images/banner/image4.jpg\" caption=\"How Safe is Your Data?\" click=\"\" />\n", "<photo url=\"images/banner/image5.jpg\" caption=\"World Class\" click=\"\" />\n", "<photo url=\"images/banner/image6.jpg\" caption=\"Knowledge\" click=\"\" />\n", "<photo url=\"images/banner/image7.jpg\" caption=\"Breathe Easy\" click=\"\" />\n", "<photo url=\"images/banner/image8.jpg\" caption=\"Reliability\" click=\"\" />\n", "<photo url=\"images/banner/image9.jpg\" caption=\"Got Fibre?\" click=\"\" />\n", "<photo url=\"images/banner/image11.jpg\" caption=\"Organization\" click=\"\" />\n", "<photo url=\"images/banner/image12.jpg\" caption=\"Security\" click=\"\" />\n", "<photo url=\"images/banner/image13.jpg\" caption=\"Downtime IS Money\" click=\"\" />\n", "<photo url=\"images/banner/image14.jpg\" caption=\"Longevity\" click=\"\" />\n", "<photo url=\"images/banner/image15.jpg\" caption=\"Dependability\" click=\"\" />\n", "<photo url=\"images/banner/image16.jpg\" caption=\"Stability\" click=\"\" />\n"); $build = " <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <main delay_time=\"7\" imgs_width=\"570\" imgs_height=\"300\" transition_speed=\"1.6\"> <slideshow>"; //Here probably a while loop to pick items from the array at random $build .= "</slideshow> </main>"; echo $build; ?> Link to comment https://forums.phpfreaks.com/topic/140305-solved-select-random-unique-items-from-an-array/ Share on other sites More sharing options...
corbin Posted January 10, 2009 Share Posted January 10, 2009 $array = array(1,2,3); $rand_elem = rand(0, count($array)-1); You will of course need some way to keep track of which ones have been picked though. I'm guessing you want the non-picked thing to persist over page views? In that case you will need to use a database or sessions or just a flat file. Link to comment https://forums.phpfreaks.com/topic/140305-solved-select-random-unique-items-from-an-array/#findComment-734159 Share on other sites More sharing options...
rondog Posted January 10, 2009 Author Share Posted January 10, 2009 you know what, I just applied shuffle($items) and then echoed out each value and it does exactly what i want Link to comment https://forums.phpfreaks.com/topic/140305-solved-select-random-unique-items-from-an-array/#findComment-734165 Share on other sites More sharing options...
corbin Posted January 10, 2009 Share Posted January 10, 2009 Ahhh.... What ever works ;p. Link to comment https://forums.phpfreaks.com/topic/140305-solved-select-random-unique-items-from-an-array/#findComment-734180 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.