我一直试图从数组中选择随机项而不重复相同的项目。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>';
}
$ value,而不是$ key。 '$图像[$值] [ '猫']' – goat