Jump to content

[SOLVED] select random unique items from an array


rondog

Recommended Posts

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;

?>

$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.

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.