2012-12-21 91 views
2

我一直试图从数组中选择随机项而不重复相同的项目。php从数组中选择多个随机密钥

样品阵列

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

我曾尝试以下,但由于某种原因,将无法正常工作。它仅将数组中的第一项收集到所呈现的计数中。 // $ adDisplay是一个数字作为值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

$ value,而不是$ key。 '$图像[$值] [ '猫']' – goat

回答

3

许多这样做的方法,我可能会洗牌,然后切片的数组:

$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>'; 
} 
0

array_rand返回随机。你实际上想要使用$images[$value]['cat']。另外请记住,如果请求一个项目,array_rand不返回数组;你必须特别处理。

0

关键将是索引,值将是随机变量,因此使用值而不是键作为您的图像数组的索引。干杯。

0

使用shuffle如果你希望你的阵列以随机顺序:

shuffle($images); 

foreach($images as $img) { 
    echo($img['cat']); 
} 

或者使用array_rand来获得一个随机密钥:

$key = array_rand($images); 

echo($images[$key]['cat']); 
0

你可以简单地使用array_rand并存储最新的关键变量上或其他地方

如果random_key == last_used_key然后 Random_key + 1

比:-)不是更难

0

替代建议:

$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>'; 
} 

所以,挑选一个随机键,将该记录从数组中移除,显示它并重新排列数组以便下一个回合。一直持续到显示足够的数量或者记录用完。