3

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>';
}
1
  • 1
    $value, not $key. $images[$value]['cat'] Commented Dec 21, 2012 at 22:13

6 Answers 6

3

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>';
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

0

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']);

Comments

0

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 :-)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.