Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

button that displays random array element

General discussions related to php

Moderators: macek, egami, gesf

button that displays random array element

Postby thelord104 » Mon Nov 26, 2012 7:41 am

i need a button that when pressed will go into my array and display a random element of it. any help would be much appreciated
thelord104
New php-forum User
New php-forum User
 
Posts: 1
Joined: Mon Nov 26, 2012 7:36 am

Re: button that displays random array element

Postby Snaek » Tue Jan 01, 2013 9:35 pm

Code: Select all
<?php
$some_array = ["bob", "john", "billy", "susan", "sandy"]; // example array
$rand_num = rand(0,sizeof($some_array)-1); // generating random number between 0 and the length of the array
echo $some_array[$rand_num]; // displaying "a random element of the array"
?>

Hi, the code above is pretty self-explanatory, it's just using the rand() function built in to php.

Hope this helped!
Snaek
New php-forum User
New php-forum User
 
Posts: 11
Joined: Sat Dec 29, 2012 6:02 pm


Return to PHP General

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.