English 中文(简体)
阵列——宽()阵列以加以扼杀?
原标题:array_rand() array to string?

我有一阵列,例如:

$hex = array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");

我想把6个随机元素作为扼杀(如1a3564):

$random_color = array_rand($hex,6);

我认为,如果 $花钱的话,就会有:

echo implode($random_color);

但是,阵列(rand)储存着母体阵列中元素的位置,而不是这些阵列元素,因此,我获得这样的东西:

259111213 instead of 259bcd.

I know this does exactly what I want:

echo $hex[$random_color[0]];
echo $hex[$random_color[1]];
echo $hex[$random_color[2]];
echo $hex[$random_color[3]];
echo $hex[$random_color[4]];
echo $hex[$random_color[5]];

但:

  • 是否有办法储存阵列中的阵列元素? 为什么它首先储存一些单位而不是一些要素?

  • 我想要取得什么成就的最佳途径是什么?

  • 为什么阵列——大() NEVER公司选择一封信作为第一个要素,几乎从来不算是第二/第三(99%的制成色像11111a 12345c 123456)?

最佳回答

you are close try this:

$hex = array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
shuffle($hex);

echo sub_str(implode(  ,$hex),0,6);
问题回答

Random colors should be generated in simplier way:

printf( %02x%02x%02x ,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));

or

printf( %06x ,mt_rand(0,16777215));

如果你需要将彩色与变数相隔,则使用http://php.net/manual/en/Function.sprintf.php>sprintf<<<>a>。 而不是

rel=“nofollow”>,见人工,Return Values

为了按预期开展工作,使用http://php.net/manual/en/Function.array-flip.php” rel=“nofollow”>array_flip(,以检索钥匙:

$random_color = array_rand(array_flip($hex), 6);

As for the "strange" results where there are almost no letters first elements, IDEOne and my server seem to reproduce these findings. A local machine running in my office (still running Debian etch / PHP 5.2.9) seems to disagree and evenly distribute elements from $hex... Seems to be a PHP version thing?

如果你只想随机扼杀数位数,你也可以做这样的事情。

substr(md5(time()),-6);

substr(md5(uniqid()),-6);

如果不与阵列的各位沟通,你就会取得类似成果。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签