简体   繁体   English

显示数组中的唯一链接

[英]show unique links from array

I have a function that shows a random banner from array: 我有一个显示数组随机横幅的函数:

//func.php
function rand_rek($rek_array){
$numberOfBanners = count($rek_array);
$numberOfBanners = $numberOfBanners - 1;
$randomBanner = rand(0,$numberOfBanners);
$rek = $rek_array[$randomBanner];
return $rek;
}

I have $reklamas array, that contains 3 banners: 我有$reklamas数组,其中包含3个横幅:

//ads.php
$reklamas = array($rek1, $rek2, $rek3);

if $_GET["noa"] isnt true, I want to add more banners to $reklamas array: 如果$ _GET [“ noa”]不是true,我想向$ reklamas数组添加更多横幅:

if (!isset($_GET["noa"]))
array_push($reklamas, $rek_adc1, $rek_adc2, $rek_adc3, $rek_adc4);

And I want to display one of random banners x times: 我想显示x随机横幅之一:

for ($i=0;$i<$banneri;$i++) {
    echo rand_rek($reklamas);
    }

The problem: These can be repeated as many times as they want array($rek1, $rek2, $rek3); 问题:这些array($rek1, $rek2, $rek3);可以重复任意多次array($rek1, $rek2, $rek3); , while these array_push($reklamas, $rek_adc1, $rek_adc2, $rek_adc3, $rek_adc4); ,而这些array_push($reklamas, $rek_adc1, $rek_adc2, $rek_adc3, $rek_adc4); can be each showed only 1 time. 每次只能显示1次。

function rand_rek() is in func.php and it is being included from ads.php where is the rest of the code. 函数rand_rek()在func.php中,并且在ads.php中,其中包括其余代码。

I think you're probably going about the problem all wrong, but in any case a solution could be 我认为您可能正在解决所有错误的问题,但是在任何情况下都可以找到解决方案

function rand_rek($rek_array){
    $numberOfBanners = count($rek_array);
    $numberOfBanners = $numberOfBanners - 1;
    $randomBanner = rand(0,$numberOfBanners);
    $rek = $rek_array[$randomBanner];
    return [$randomBanner, $rek];
}

list($bannerNum, $banner) = rand_rek($reklamas);
if ($bannerNum < 3) {
    $repeats = $x;
} else {
    $repeats = 1;
}

for ($i=0;$i<$repeats;$i++) {
    echo $banner;
}

Which retrieves the index of the banner and checks it before running the loop. 它在运行循环之前检索横幅的索引并对其进行检查。 I've also used your value $x that you mention in the text but not in your code. 我还使用了您在文本中提及但在代码中未提及的$x值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM