Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP สร้างอาร์เรย์ตัวเลขสุ่มไม่ซ้ำ (Unique Random Numbers)

สำหรับอาร์เรย์ตัวเลขสุ่มไม่ซ้ำ ใช้ range() พร้อม shuffle()

ตัวอย่าง

โค้ด PHP ดังนี้

<!DOCTYPE html> <html> <body> <?php 
$limit_random_array_values = range(10, 20); 
shuffle($limit_random_array_values); 
$all_five_random_array_value = array_slice($limit_random_array_values ,0,5); 
print_r($all_five_random_array_value); 
?> </body> </html>

ผลลัพธ์

นี่จะผลิตเอาต์พุตดังนี้

Array ( [0] => 11 [1] => 14 [2] => 15 [3] => 12 [4] => 18 )