Tuesday, February 8, 2011

Use random in an array-PHP


Assalamu'alaikum and selamat sejahtera

For this post,we will discuss on "array_rand( the name of your array,total of random keys that you want)" function.For example:

<?PHP
$number = array(1 => 16 ,  2 => 13 ,  3 => 44 ,  4 => 74 ,  5 => 50 ,  6 => 63);
$random_numbers = array_rand($number, 1);
print $random_numbers;
?>


PHP will get the key name (what is key name?refer on previous post ).So it only print the key name only.To get the random value of  an array ,change this line:

print $random_numbers;


to be like this:


print $number[$random_numbers];


The code:


<html>

<head>

<title>Randoms-Array</title>

<?PHP

$number = array(1 => 16, 2 => 13, 3 => 44, 4 => 74, 5 => 50, 6 => 63);

$random_numbers = array_rand($number,1);

print $random_numbers;


?>

</head>

<body>

</body>

</html>


So,that's all

N/NOTE:any confusing??

No comments:

Post a Comment