Home  >  Article  >  Backend Development  >  How to use an array to generate a verification code?

How to use an array to generate a verification code?

autoload
autoloadOriginal
2021-03-09 17:11:342431browse

1. Several array functions that need to be used (using the code examples in 2):

a.range(start value, end value): Generate an array within the specified range.

<?php
   $array=range(&#39;a&#39;,&#39;z&#39;);//生成一个从a-z的数组
?>

b.<strong>array_merge</strong>(): Merge one or more arrays.

Subscript conflicts are divided into two situations:

                                                                                                                      ’ ’ ’ ’ s ’ s ’s ’ s ‐                        The element overwrites the previous element

<?php
   $array1=range(&#39;a&#39;,&#39;z&#39;);
   $array2=rrange(0,9);
   $array3=range(&#39;f&#39;,&#39;z&#39;);
   $array=array_merge($array1,$array2,$array3);//将$array1,$array2,$array3合并至$array

c.

array_rand(The input array specifies how many units you want to remove): Remove one or more units from the array random cells and returns one or more keys of random entries. If only one is taken out, array_rand() returns the key name of the random unit. Otherwise, an array containing random keys is returned. Once completed, you can get the random value of the array based on the random key. If the retrieved quantity exceeds the length of array, an E_WARNING error will occur and NULL will be returned.

<?php
      $index=array_rand($array,4);//从$array中取出4个单元
     

d.

shuffle(Array to be operated): Shuffle (randomly arrange the order of units) an array.

<?php
      shuffle($index);//打乱下标

2. Use array functions:

<?php
$array=range(&#39;a&#39;,&#39;z&#39;);

$array=array_merge(range(&#39;a&#39;,&#39;z&#39;),range(&#39;A&#39;,&#39;Z&#39;),range(0,9),range(&#39;f&#39;,&#39;z&#39;));

$index=array_rand($array,4);
shuffle($index);
$str=&#39;&#39;;
foreach($index as $i){
   $str.=$array[$i];
}
echo $str;
Recommended:

php tutorial

,php video tutorial

The above is the detailed content of How to use an array to generate a verification code?. 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