php从数组中选择多个随机键
我一直在尝试从数组中随机选择项目,而不重复相同的项目 样本阵列php从数组中选择多个随机键,php,arrays,random,Php,Arrays,Random,我一直在尝试从数组中随机选择项目,而不重复相同的项目 样本阵列 $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[]=
$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>';
}
$rand=array\u rand($images,$adDisplay);
foreach($rand as$key=>$value){
回声';
}
array\u rand
将随机键作为值返回。您实际上想要使用$images[$value]['cat']
。还要记住,array\u rand
在请求一个项目时不会返回数组;您必须特别处理此问题。键将是索引,值将是随机变量,因此在图像数组中使用值而不是键作为索引。干杯。要做到这一点,我可能会洗牌,然后切片阵列:
$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>';
}
$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\u attire.php');
$images[]=array('img'=>'limos.jpg','cat'=>'limos_transportaion.php');
$images[]=array('img'=>'photography.jpg','cat'=>'photography.php');
$images[]=array('img'=>'vinces.jpg','cat'=>'vinces.php');
$images[]=array('img'=>'widding_planer.jpg','cat'=>'planning.php');
洗牌(图像);
$adDisplay=5;
foreach(数组\u切片($images,0,$adDisplay)作为$image){
回声';
}
如果希望数组以随机顺序排列,请使用随机排列:
shuffle($images);
foreach($images as $img) {
echo($img['cat']);
}
或者使用array\u rand
获取随机键:
$key = array_rand($images);
echo($images[$key]['cat']);
您可以简单地使用array_rand并将最新的键存储在变量或某个地方
如果随机密钥==上次使用的密钥,则
随机_键+1
不比这更难:-)备选建议:
$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>';
}
$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\u attire.php');
$images[]=array('img'=>'limos.jpg','cat'=>'limos_transportaion.php');
$images[]=array('img'=>'photography.jpg','cat'=>'photography.php');
$images[]=array('img'=>'vinces.jpg','cat'=>'vinces.php');
$images[]=array('img'=>'widding_planer.jpg','cat'=>'planning.php');
对于($i=0;$i<$adDisplay和sizeof($images)>1;$i++){
$k=阵列随机数(0,大小($images)-1);
$image=$images[$k];
未设置($images[$k];
排序(图像);
回声';
}
因此,选择一个随机键,从数组中删除该记录,显示该记录,并为下一个循环对数组重新排序。继续,直到显示足够的记录或记录用尽为止。$value,而不是$key。$images[$value]['cat']