PHP Questions Home
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

How do I pick a random value from an array?


If you want to choose an entry randomly from an array then you can use the array_rand function like this:

<?php

$lottery 
range(1,49);

$onenumber array_rand($lottery);
echo 
"One number is: $onenumber\n\n";
?>


This will print a random number between 1 and 49.

You can also use array_rand to return a certain number of random numbers.

People often like to write a little computer program to generate their weekly lottery numbers for them. Here's how to create a lottery number generator for the UK lottery that picks six random numbers between 1 and 49 in just one line of PHP code:

<?php
foreach(array_rand(range(1,49),6) as $number) echo "$number ";
?>


This will print a random selection of six numbers to the screen, for instance:
27 33 48 21 19 43



ASK A QUESTION

More arrays PHP Questions

How can I mix up the order of values in an array?

How can I sort a multi-dimensional array?

How can I display a 2 dimensonal array?

What is the starting salary of Php developer in india?? after 1 year experience how much i could get??

I have the following list : Tom T1 Tom T2 Bill T1 Bill T2 Jack T1 Jack T2 I need it converted into : Tom T1,T2 Bill T1,T2 Jack T1,T2



Custom Search