显示和随机化PHP阵列

时间:2013-12-09 11:23:15

标签: php mysql arrays

我有一个显示结果的数组如下:

Array
(
    [0] => 71
    [1] => 56
    [2] => 64
    [3] => 82
    [4] => 90
    [5] => 80
    [6] => 65
    [7] => 62
    [8] => 14
    [9] => 3
)

我的代码是:

while($row = mysql_fetch_assoc($result))
{   

  $show[] = $row['number'];

}
   echo '<pre>'; print_r($show);

如何随机化数组内容并将其显示为仅5位数e:g 71 64 14 80 82

4 个答案:

答案 0 :(得分:6)

$rand5 = array_rand(array_flip($show), 5);
// we need array_flip because array_rand returns the keys

http://php.net/array_rand

http://php.net/array_flip

答案 1 :(得分:4)

我会用shuffle来随机化数组,因为获取数组的5个元素,这实际上取决于你的需要,你想要完成什么,得到前5个元素,最后一些,或其他一些逻辑。

答案 2 :(得分:2)

试试这个:

$arr=array();
for ($i=0;$i<5;$i++)
{
$arr[i]=$items[array_rand($items)];
}

$arr包含结果

答案 3 :(得分:0)

在查询中不确定是否有这个,但您也可以使用

SELECT number
FROM SomeTable
ORDER BY RAND()
LIMIT 5

但请注意,ORDER BY RAND()效率非常低,因此仅在处理少量行时才适用。但是,如果要返回大量行,那么在PHP中执行它也会效率低下。

有更有效的方法可以返回单个随机行,因此如果您有非常多的行,则可以使用5次有效检索单行并将结果合并在一起(尽管您需要考虑到随机行可能是重复的可能性。

例如(并不是很满意): -

(SELECT r1.number
FROM SomeTable AS r1 
INNER JOIN 
(
    SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
    FROM SomeTable
) AS r2
ON r1.number >= r2.number
ORDER BY r1.number ASC
LIMIT 1)
UNION
(SELECT r1.number
FROM SomeTable AS r1 
INNER JOIN 
(
    SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
    FROM SomeTable
) AS r2
ON r1.number >= r2.number
ORDER BY r1.number ASC
LIMIT 1)
UNION
(SELECT r1.number
FROM SomeTable AS r1 
INNER JOIN 
(
    SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
    FROM SomeTable
) AS r2
ON r1.number >= r2.number
ORDER BY r1.number ASC
LIMIT 1)
UNION
(SELECT r1.number
FROM SomeTable AS r1 
INNER JOIN 
(
    SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
    FROM SomeTable
) AS r2
ON r1.number >= r2.number
ORDER BY r1.number ASC
LIMIT 1)
UNION
(SELECT r1.number
FROM SomeTable AS r1 
INNER JOIN 
(
    SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
    FROM SomeTable
) AS r2
ON r1.number >= r2.number
ORDER BY r1.number ASC
LIMIT 1)
UNION
(SELECT r1.number
FROM SomeTable AS r1 
INNER JOIN 
(
    SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
    FROM SomeTable
) AS r2
ON r1.number >= r2.number
ORDER BY r1.number ASC
LIMIT 1)
UNION
(SELECT r1.number
FROM SomeTable AS r1 
INNER JOIN 
(
    SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
    FROM SomeTable
) AS r2
ON r1.number >= r2.number
ORDER BY r1.number ASC
LIMIT 1)
UNION
(SELECT r1.number
FROM SomeTable AS r1 
INNER JOIN 
(
    SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
    FROM SomeTable
) AS r2
ON r1.number >= r2.number
ORDER BY r1.number ASC
LIMIT 1)
UNION
(SELECT r1.number
FROM SomeTable AS r1 
INNER JOIN 
(
    SELECT RAND( ) * ( MAX( number ) - MIN( number ) ) + MIN( number ) AS number
    FROM SomeTable
) AS r2
ON r1.number >= r2.number
ORDER BY r1.number ASC
LIMIT 1)
LIMIT 5

SQL小提琴: -

http://sqlfiddle.com/#!2/14caa/4

这将获得10个随机行,然后使用另一个LIMIT 5来获得仅5行。当然,这可能不会得到5行的可能性很小(不太可能,但是这些联合查询中的每一个都可能会返回同一行)