Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple PHP random array [duplicate]

Tags:

arrays

php

I have a simple array like this:

$input = array('Line1', 'Line2', 'Line3');

And want to echo one of the values randomly. I've done this before but can't remember how I did it and all the examples of array_rand seem more complex that what I need.

Can any help? Thanks

like image 360
Cameron Avatar asked Nov 22 '10 00:11

Cameron


2 Answers

echo $input[array_rand($input)];

array_rand() returns the key, so we need to plug it back into $input to get the value.

like image 135
waiwai933 Avatar answered Oct 06 '22 21:10

waiwai933


Complex? Are we on the same manual page?

$rand_key = array_rand($input, 1);
like image 35
Pekka Avatar answered Oct 06 '22 21:10

Pekka