Home  >  Article  >  Backend Development  >  Random number generation from array in PHP

Random number generation from array in PHP

不言
不言Original
2018-04-19 10:05:271606browse

The content of this article is about the random number generation of arrays in PHP. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it

<?php
// array_rand();从数组中随机取出一个或多个单元
// 如果array_rand不写length参数或者参数是1,返回的不是数组,而是int类型的整数,如下的var_dump,这样的话赋值给变量再array_merge就会报错。
// var_dump(array_rand(range(0,9),$length));
// array_flip();交换数组中的键和值
// range();根据范围创建数组,包含指定的元素
// shuffle — 打乱数组,返回的是布尔值,不适合本案例
$merge=array_rand(array_flip(array_merge(range(&#39;a&#39;,&#39;z&#39;),range(0,9))),4);
print_r($merge);
 ?>
<?php
// join(&#39;&#39;,$array)以空连接数组,并返回字符串,join是implode的别名
$str=join(&#39;&#39;,array_rand(array_flip(array_merge(range(&#39;a&#39;,&#39;z&#39;),range(0,9))),4));
var_dump($str);
 ?>

Related recommendations:

Notes on arrays and strings in PHP


The above is the detailed content of Random number generation from array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn