Get random item from array [duplicate]

2019-01-03 22:45发布

This question already has an answer here:

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

Each item of this array is some number.

How do I get random item from $items?

标签: php random
4条回答
Explosion°爆炸
2楼-- · 2019-01-03 23:03

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

查看更多
ら.Afraid
3楼-- · 2019-01-03 23:06
姐就是有狂的资本
4楼-- · 2019-01-03 23:07

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

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

查看更多
Deceive 欺骗
5楼-- · 2019-01-03 23:20
echo $items[array_rand($items)];

array_rand()

查看更多
登录 后发表回答