Reputation: 281
I have been trying to select random items from an array without repeating the same item.
Sample Array
$images=array();
$images[]=array('img'=>'bands.jpg','cat'=>'bands.php');
$images[]=array('img'=>'cake.jpg','cat'=>'cakes.php');
$images[]=array('img'=>'catering.jpg','cat'=>'catering.php');
$images[]=array('img'=>'dj.jpg','cat'=>'djs.php');
$images[]=array('img'=>'dress.jpg','cat'=>'dress_attire.php');
$images[]=array('img'=>'limos.jpg','cat'=>'limos_transportaion.php');
$images[]=array('img'=>'photography.jpg','cat'=>'photography.php');
$images[]=array('img'=>'venues.jpg','cat'=>'venues.php');
$images[]=array('img'=>'wedding_planer.jpg','cat'=>'planning.php');
I have tried the following but for some reason it will not work correctly. It is only collecting the first items in the array to the count rendered. // $adDisplay is a number between 1-9
$rand = array_rand($images, $adDisplay);
foreach($rand as $key => $value){
echo'<a href="'.$images[$key]['cat'].'"><img src="img/banners/'.$images[$key]['img'].'" border="0" alt="" /></a>';
}
Upvotes: 3
Views: 1783
Reputation: 1132
Alternative suggestion:
$images = array();
$images[]=array('img'=>'bands.jpg','cat'=>'bands.php');
$images[]=array('img'=>'cake.jpg','cat'=>'cakes.php');
$images[]=array('img'=>'catering.jpg','cat'=>'catering.php');
$images[]=array('img'=>'dj.jpg','cat'=>'djs.php');
$images[]=array('img'=>'dress.jpg','cat'=>'dress_attire.php');
$images[]=array('img'=>'limos.jpg','cat'=>'limos_transportaion.php');
$images[]=array('img'=>'photography.jpg','cat'=>'photography.php');
$images[]=array('img'=>'venues.jpg','cat'=>'venues.php');
$images[]=array('img'=>'wedding_planer.jpg','cat'=>'planning.php');
for ($i = 0; $i < $adDisplay AND sizeof($images) > 1; $i++) {
$k = array_rand(0, sizeof($images)-1);
$image = $images[$k];
unset($images[$k];
sort($images);
echo '<a href="' . htmlspecialchars($image['cat']) . '">'
. '<img src="img/banners/'
. htmlspecialchars($image['img']) . ' border="0" alt="" />'
. '</a>';
}
So, picks a random key, removes that record from the array, displays it and re-sorts the array for the next go-round. Continues until enough have been displayed or it runs out of records.
Upvotes: 0
Reputation: 1
You can simply use array_rand and store latest key on variable or somewhere
If random_key == last_used_key then Random_key+1
Not harder than that :-)
Upvotes: 0
Reputation: 1016
Use shuffle
if you want your array in random order:
shuffle($images);
foreach($images as $img) {
echo($img['cat']);
}
Or use array_rand
to get a random key:
$key = array_rand($images);
echo($images[$key]['cat']);
Upvotes: 0
Reputation: 111
Many ways of doing this, I'd probably shuffle and then slice the array:
$images = array();
$images[]=array('img'=>'bands.jpg','cat'=>'bands.php');
$images[]=array('img'=>'cake.jpg','cat'=>'cakes.php');
$images[]=array('img'=>'catering.jpg','cat'=>'catering.php');
$images[]=array('img'=>'dj.jpg','cat'=>'djs.php');
$images[]=array('img'=>'dress.jpg','cat'=>'dress_attire.php');
$images[]=array('img'=>'limos.jpg','cat'=>'limos_transportaion.php');
$images[]=array('img'=>'photography.jpg','cat'=>'photography.php');
$images[]=array('img'=>'venues.jpg','cat'=>'venues.php');
$images[]=array('img'=>'wedding_planer.jpg','cat'=>'planning.php');
shuffle($images);
$adDisplay = 5;
foreach( array_slice($images,0,$adDisplay) as $image){
echo '<a href="' . htmlspecialchars($image['cat']) . '">'
. '<img src="img/banners/'
. htmlspecialchars($image['img']) . ' border="0" alt="" />'
. '</a>';
}
Upvotes: 3
Reputation: 43
The key will be the index, value will be the random variable, thus use value instead of key as the index into your images array. Cheers.
Upvotes: 0
Reputation: 191729
array_rand
returns the random keys as values. You actually want to use $images[$value]['cat']
. Also keep in mind that array_rand
does not return an array if one item is requested; you must handle that specially.
Upvotes: 0