Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get random item from array [duplicate]

Tags:

php

random

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