programing

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

randomtip 2023. 1. 25. 08:42
반응형

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

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

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

에서 랜덤 아이템을 가져오려면 어떻게 해야 하나요?$items?

echo $items[array_rand($items)];

어레이_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

반응형