|
 |
|
สุ่มค่าแบบไม่ซ้ำ ทำอย่างไรค่ะ มีวิธการสุ่มค่าใน array แบบไม่ซ้ำไหมค่ะ พอดีต้องทำส่งอาจารย์ค่ะ |
|
 |
|
|
 |
 |
|
<?php
$array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
$output = array_rand( $array , 2 );
print_r( $output );
?>
ผลลัพธ์ :
Array
(
[0] => 3
[1] => 8
)
|
 |
 |
 |
 |
Date :
2009-08-14 22:24:03 |
By :
ranarong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าค่าในอาเรย์มันก็ต้องซ้ำนะ หรือว่าจะเอาไม่ซ้ำ เอาซ้ำนะถึงแม้ว่าในอาเรย์จะซ้ำ
Code (PHP)
<?php
$arr = array(1, 9, 2, 3, 4, 7, 1);
$last_index = count($arr) - 1;
$indexs = array();
do{
$rand = rand($last_index);
if( !in_array($rand, $indexs) )
array_push($rand);
$i++;
}while($i < $last_index);
// แสดงค่า
foreach($indexs as $arr_index)
echo $arr[$arr_index];
?>
ปล. ไม่ได้ทดสอบนะครับ
http://gunner.freetzi.com/newVer/
|
 |
 |
 |
 |
Date :
2009-08-14 22:25:18 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่เคยรู้จักเลย array_rand() เขียนตั้งยาว 
|
 |
 |
 |
 |
Date :
2009-08-14 22:28:00 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนแรกผมก็ไม่รู้หรอกครับ ถามอาจารย์ google เอา อิ อิ
ผมว่าอาจารย์ google นี่เป็นสุดยอดอาจารย์ทางด้านความรู้
แล้วละครับ ถ้าคิด keyword ในการ search เป็นก็จะได้
ข้อมูลที่เราต้องการแน่นอนครับ
|
 |
 |
 |
 |
Date :
2009-08-14 23:11:55 |
By :
ranarong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่อั๋นนั่นหล่ะ 5555+
เดี๋ยวๆๆ หาหลักฐานก่อน อยู่ไหนหว่าาา ???? ก๊ากๆ
|
 |
 |
 |
 |
Date :
2009-08-15 00:04:45 |
By :
panyapol |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|