Trong khuôn khổ của đoạn trích này, chúng tôi sẽ giải thích một số cách tạo một chuỗi ngẫu nhiên, duy nhất với sự trợ giúp của kho vũ khí PHP
Cách thứ nhất là dùng vũ lực. Đây là phương pháp đơn giản nhất có thể đạt được bằng cách làm theo các bước dưới đây
- Lưu trữ tất cả các chữ cái có thể thành chuỗi
- Tạo chỉ mục ngẫu nhiên từ 0 đến độ dài của chuỗi -1
- In chữ cái trên chỉ mục đã cho
- Thực hiện các bước n lần [n được coi là độ dài của chuỗi cần thiết]
Đây là một ví dụ
Here is the first output:
And, the second one:
In PHP, there are several functions such as md5[], sha1[], and hash[] that might be applied for hashing a string based on exact algorithms such as “sha1”, “sha256”, “md5”, and so on.
All of these functions are capable of taking a string as an argument and output an Alpha-Numeric hashed string.
After understanding how to utilize the functions, the next steps become simpler:
- Generating a random number with the rand[] function.
- Hashing it with one of the functions above.
Here is an example:
________0
The output will look like this:
________2
This is an inbuilt function that is applied for generating a unique ID built on the current time in microseconds. It returns a 13-character long unique string, by default.
The example is demonstrated below:
________0
The first output is the following:
Here is the second one:
Please, take into consideration that all the methods above are based on rand[] and uniqid[] functions. However, they are not considered cryptographically secure random generators.
If you want to generate cryptographically secure pseudo random bytes that can be converted into a hexadecimal format with the bin2hex[] function, then you should use the random_bytes function.
Here is an example:
________0
Here is the first output:
________5
The second one:
________6
So, in this snippet, we represented several ways to generate a random string with PHP. Learning them will make your work easier and more productive.
Để tạo chuỗi ký tự chữ số ngẫu nhiên có nhiều cách để tạo chuỗi ký tự chữ số ngẫu nhiên, duy nhất trong PHP. Các cách sau để tạo chuỗi chữ và số ngẫu nhiên
Sử dụng hàm str_shuffle[].
Hàm str_shuffle[] là một hàm có sẵn trong PHP và được sử dụng để xáo trộn ngẫu nhiên tất cả các ký tự của chuỗi được truyền cho hàm dưới dạng tham số. Khi một số được chuyển, nó coi số đó là chuỗi và xáo trộn nó. Hàm này không thực hiện bất kỳ thay đổi nào trong chuỗi gốc hoặc số được truyền cho nó dưới dạng tham số. Thay vào đó, nó trả về một chuỗi mới là một trong những hoán vị có thể có của chuỗi được truyền cho nó trong tham số
Thí dụ
$length = 10; $data = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz'; echo substr[str_shuffle[$used_symbols], 0, $length]; echo "
"; echo substr[str_shuffle[$used_symbols], 0, 8];
đầu ra
2 năm fd5eIFrh
ujwRe9k2
Sử dụng hàm md5[]
Hàm md5[] được sử dụng để tính hàm băm MD5 của một chuỗi. Truyền dấu thời gian làm đối số và hàm md5 sẽ chuyển đổi chúng thành ký tự 32 bit
Create multidimensional array unique for any single key index.
e.g I want to create multi dimentional unique array for specific code
Code :
My array is like this,
$details = array[
0 => array["id"=>"1", "name"=>"Mike", "num"=>"9876543210"],
1 => array["id"=>"2", "name"=>"Carissa", "num"=>"08548596258"],
2 => array["id"=>"1", "name"=>"Mathew", "num"=>"784581254"],
];
?>
You can make it unique for any field like id, name or num.
I have develop this function for same :
function unique_multidim_array[$array, $key] {
$temp_array = array[];
$i = 0;
$key_array = array[];
________số 8_______
Now, call this function anywhere from your code,
something like this,
$details = unique_multidim_array[$details,'id'];
?>
Output will be like this :
$details = array[
0 => array["id"=>"1","name"=>"Mike","num"=>"9876543210"],
1 => array["id"=>"2","name"=>"Carissa","num"=>"08548596258"],
];
?>