Php 如果相同的随机数重复,则继续循环

Php 如果相同的随机数重复,则继续循环,php,Php,我想从MySQL数据库生成随机文章,结果是5篇不同的文章。我尝试使用preg_match() 我还尝试使用strpos() 但仍然生成相同的随机数。比如26 12 2 6,13 9 13 3,11 13 12 11。我认为我没有正确使用该函数,或者代码流中有错误。在检查是否重复之前,您正在将新的数字添加到结果中。因此,您的if条件将始终为false,您将永远无法获得文章 在if块中移动这些行: $random_post--; $unique_number .= " ".$rand_id; 在检

我想从MySQL数据库生成随机文章,结果是5篇不同的文章。我尝试使用preg_match()

我还尝试使用strpos()


但仍然生成相同的随机数。比如26 12 2 6,13 9 13 3,11 13 12 11。我认为我没有正确使用该函数,或者代码流中有错误。

在检查是否重复之前,您正在将新的数字添加到结果中。因此,您的
if
条件将始终为false,您将永远无法获得文章

if
块中移动这些行:

$random_post--;
$unique_number .= " ".$rand_id;

在检查结果是否重复之前,您正在将新数字添加到结果中。因此,您的
if
条件将始终为false,您将永远无法获得文章

if
块中移动这些行:

$random_post--;
$unique_number .= " ".$rand_id;

为什么要使用字符串保存此数据?我建议使用数组:

$random_post = 5;
$unique_number = array();

do {

    $rand_id = rand($min_range, $max_range);

        if (!in_array($rand_id, $unique_number)) {
            get_article($rand_id);  
            $unique_number[] = $rand_id;
            $random_post--;
        }


} while ($random_post);
如果出于其他目的需要由这些数字组成的字符串,可以使用内爆:

$unique_string = implode(" ", $unique_number);

为什么要使用字符串保存此数据?我建议使用数组:

$random_post = 5;
$unique_number = array();

do {

    $rand_id = rand($min_range, $max_range);

        if (!in_array($rand_id, $unique_number)) {
            get_article($rand_id);  
            $unique_number[] = $rand_id;
            $random_post--;
        }


} while ($random_post);
如果出于其他目的需要由这些数字组成的字符串,可以使用内爆:

$unique_string = implode(" ", $unique_number);

试试这个:它将只使用
rand
所需的次数

//Create an array with available range
$items = range($min_range, $max_range);
//Number of posts
$posts = 5
//Final list
$answer = array();

for($x = 0; $x < 5; $x++) {
    //Generate a random number from 0 to number of items available in the array
    $rand = rand(0, count($items) - 1);
    //Get the item at the given random position
    $answer[] = $items[$rand];
    //Remove the used item from the array
    array_splice($items, $rand, 1);
}
//创建具有可用范围的数组
$items=范围($min\u范围,$max\u范围);
//员额数目
$posts=5
//最终名单
$answer=array();
对于($x=0;$x<5;$x++){
//生成一个从0到数组中可用项数的随机数
$rand=rand(0,计数($items)-1);
//在给定的随机位置获取项目
$answer[]=$items[$rand];
//从数组中删除已使用的项
阵列拼接($items,$rand,1);
}

上面的示例将只生成5个随机数,它将为您提供所需的ID。

试试这个:它将只使用
rand
所需的次数

//Create an array with available range
$items = range($min_range, $max_range);
//Number of posts
$posts = 5
//Final list
$answer = array();

for($x = 0; $x < 5; $x++) {
    //Generate a random number from 0 to number of items available in the array
    $rand = rand(0, count($items) - 1);
    //Get the item at the given random position
    $answer[] = $items[$rand];
    //Remove the used item from the array
    array_splice($items, $rand, 1);
}
//创建具有可用范围的数组
$items=范围($min\u范围,$max\u范围);
//员额数目
$posts=5
//最终名单
$answer=array();
对于($x=0;$x<5;$x++){
//生成一个从0到数组中可用项数的随机数
$rand=rand(0,计数($items)-1);
//在给定的随机位置获取项目
$answer[]=$items[$rand];
//从数组中删除已使用的项
阵列拼接($items,$rand,1);
}

上面的示例将只生成5个随机数,并将为您提供所需的ID。

是否必须通过PHP完成?您可以使用SQL执行类似的操作:

SELECT your, things FROM table ORDER BY RAND() LIMIT 5

它选择了5个不同的条目,从不相同两次

是否必须通过PHP完成?您可以使用SQL执行类似的操作:

SELECT your, things FROM table ORDER BY RAND() LIMIT 5

它选择了5个不同的条目,从不相同的两次

简短而简单的脚本:

$ids = range($min, $max);
shuffle($ids);
foreach(array_slice($ids, 0, $random_post) as $id)
    get_article($id);
如果你有很多文章,我会用

$ids = array();
while(count($ids) < $random_post)
{
    $id = rand($min, $max);
    if(!isset($ids[$id))
        get_article($ids[$id] = $id);
}
$ids=array();
while(计数($id)<$random\u post)
{
$id=rand($min,$max);
如果(!isset($id[$id))
获取文章($id[$id]=$id);
}

简短的脚本:

$ids = range($min, $max);
shuffle($ids);
foreach(array_slice($ids, 0, $random_post) as $id)
    get_article($id);
如果你有很多文章,我会用

$ids = array();
while(count($ids) < $random_post)
{
    $id = rand($min, $max);
    if(!isset($ids[$id))
        get_article($ids[$id] = $id);
}
$ids=array();
while(计数($id)<$random\u post)
{
$id=rand($min,$max);
如果(!isset($id[$id))
获取文章($id[$id]=$id);
}

谢谢。我没有考虑这个问题。谢谢。我没有考虑这个问题。我在某个地方读到rand()会获取所有数据,因此会减慢查询速度。因此我改为php。@Yusufmm
rand()
每行工作一次以选择一些内容,因此如果您有很多行(阅读:远远超过15000行)您将开始看到一些性能影响。这实际上取决于您的数据规模,很像此处的任何
foreach
风格函数,其他一些来源说它很慢。我仍在搜索有关您声明的一些信息。我在某个地方读到了rand()将获取所有数据,因此将减慢查询速度。因此,我改为php。@Yusufmm
RAND()
每行工作以选择一些内容,因此如果您有很多行(阅读:远远超过15000行)您将开始看到一些性能影响。这实际上取决于您的数据规模,很像此处的任何
foreach
风格函数,其他一些来源称其速度较慢。我仍在搜索有关您声明的一些信息。