[php] Get random item from array

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

Each item of this array is some number.

How do I get random item from $items?

This question is related to php random

The answer is


echo $items[array_rand($items)];

array_rand()


use array_rand()

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


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


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

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