开发者

Get random item from array [duplicate]

开发者 https://www.devze.com 2023-01-26 06:07 出处:网络
This question already has answers here: How to get random value out of an array? 开发者_JAVA百科 (21 answers)
This question already has answers here: How to get random value out of an array? 开发者_JAVA百科 (21 answers) Closed 8 years ago.
$items = Array(523,3452,334,31,...5346);

Each item of this array is some number.

How do I get random item from $items?


echo $items[array_rand($items)];

array_rand()


If you don't mind picking the same item again at some other time:

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


Use PHP Rand function

<?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";
?>

More Help


use array_rand()

see php manual -> http://php.net/manual/en/function.array-rand.php

0

精彩评论

暂无评论...
验证码 换一张
取 消