IT story

배열에서 임의 항목 가져 오기

hot-time 2020. 5. 19. 08:13
반응형

배열에서 임의 항목 가져 오기


이 질문에는 이미 답변이 있습니다.

$items = Array(523,3452,334,31,...5346);

이 배열의 각 항목은 숫자입니다.

에서 임의의 항목을 $items어떻게 습니까?


echo $items[array_rand($items)];

array_rand ()


다른 시간에 같은 항목을 다시 고르지 않아도되는 경우 :

$items[rand(0, count($items) - 1)];


PHP Rand 기능 사용

<?php
  $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
  $rand_keys = array_rand($input, 2);
  echo $input[$rand_keys[0]] . "\n";
  echo $input[$rand_keys[1]] . "\n";
?>

추가 도움말


사용하다 array_rand()

PHP 매뉴얼 참조-> http://php.net/manual/en/function.array-rand.php

참고 URL : https://stackoverflow.com/questions/4233407/get-random-item-from-array

반응형