Home > Article > Backend Development > Random number generation from array in PHP
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('a','z'),range(0,9))),4); print_r($merge); ?>
<?php // join('',$array)以空连接数组,并返回字符串,join是implode的别名 $str=join('',array_rand(array_flip(array_merge(range('a','z'),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!