PHP ¹è¿¿¡¼ ¹«ÀÛÀ§·Î Çϳª »Ì¾ÆÁÖ´Â array rand() ÇÔ¼ö |
|
|
 |
2³â Àü |
array_rand() ÇÔ¼ö´Â ¹è¿¿¡¼ ¾î´À Çϳª¸¦ ¹«ÀÛÀ§·Î »Ì¾ÆÁØ´Ù. °£´ÜÈ÷ ´ÙÀ½°ú °°ÀÌ ¾µ ¼ö ÀÖ´Ù.
$vegetable = array("½Ã±ÝÄ¡", "Äᳪ¹°", "¾Æ¿í", "³ÃÀÌ");
$selected = array_rand($vegetable);
echo $selected;
ÀÌ ¿¹Á¦ ¿¡¼´Â ä¼Ò À̸§ÀÌ µé¾îÀÖ´Â ¹è¿¿¡¼ Çϳª¸¦ »Ì°Ô µÈ´Ù.
´Ü, Ãâ·Â°ªÀº ¹«ÀÛÀ§·Î »ÌÀº °Ô ¹è¿¿¡¼ ¸î¹ø°¿¡ Àִ°¡¸¦ ³ªÅ¸³»´Â ¼ýÀÚÀÌ´Ù.
µû¶ó¼ ¾Æ·¡¿Í °°ÀÌ echo¸¦ ½á¼ ±× °á°ú¸¦ Ãâ·ÂÇÒ °æ¿ì 1, 2, 3°ú °°Àº ¼ýÀÚ°¡ ³ª¿Â´Ù.
¼ýÀÚ°¡ ¾Æ´Ï¶ó ¹è¿¿¡¼ »ÌÀº ½ÇÁ¦°ªÀ» ¾ò°í ½Í´Ù¸é ¾Æ·¡¿Í °°ÀÌ ½á¾ß ÇÑ´Ù.
echo $vegetable[$selected]; |
|
̵̧ : 150 |
̵̧
¸ñ·Ï
|
|