【问题标题】:php select multiple random keys from arrayphp从数组中选择多个随机键
【发布时间】:2012-12-21 22:09:15
【问题描述】:

我一直在尝试从数组中选择随机项而不重复相同的项。

样本数组

$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。 $images[$value]['cat']

标签: php arrays random


【解决方案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');

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

【讨论】:

    【解决方案2】:

    array_rand 将随机 keys 作为值返回。您实际上想使用$images[$value]['cat']。另请记住,如果请求一项,array_rand 不会返回数组;你必须特别处理。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        如果您希望数组以随机顺序排列,请使用 shuffle

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

        或者使用array_rand获取随机密钥:

        $key = array_rand($images);
        
        echo($images[$key]['cat']);
        

        【讨论】:

          【解决方案5】:

          您可以简单地使用 array_rand 并将最新的密钥存储在变量或某处

          如果 random_key == last_used_key 那么 随机键+1

          不比这更难:-)

          【讨论】:

            【解决方案6】:

            替代建议:

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

            因此,选择一个随机键,从数组中删除该记录,显示它并为下一轮重新排序数组。继续,直到显示足够多或记录用完为止。

            【讨论】:

              猜你喜欢
              • 2018-06-13
              • 2016-11-12
              • 2013-07-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-07-30
              • 1970-01-01
              相关资源
              最近更新 更多