显示和PHP弄乱阵列 [英] Displaying and Randomising php Arrays

查看:135
本文介绍了显示和PHP弄乱阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 阵列

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

我的code是:

 而($行= mysql_fetch_assoc($结果))
{  $显示[] = $行['号'];}
   呼应'< pre>';的print_r($显示);

我怎么可以随机数组内容并将其显示为5位数字只有E:摹 71 64 14 80 82


解决方案

  $ rand5 = array_rand(array_flip($显示),5);
//我们需要array_flip因为array_rand返回键

http://php.net/array_rand

http://php.net/array_flip

I have an array displaying results as below:

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

My code is:

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

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

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

How can I randomize the array contents and display them as 5 digits only e:g 71 64 14 80 82

解决方案

$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

这篇关于显示和PHP弄乱阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆