How to use array rand in PHP

In this article, I will explain how the array_rand() function can be used in PHP.
  • 2157

array_rand() function in PHP

  • The array_rand() function is used to returns a random entries from an array, or it returns an array of random keys if you specify that the function should return more than one key.

  • The array_rand() function fetch one or more random entries out of an array.

  • In array_rand() function, If the value of keys is 1, It return a random key.

Syntax

array_rand(array,number)

Parameter

  • array  array is required parameter. Specifies an array.

  • number number is optional parameter. It's specifie how many random keys to return.

Example

The following example show to how the array_rand() function can be used in PHP.

<html>

<body>

<h3 style="color: greenyellow;">array_rand() function example in PHP</h3>  

    <?php

    $a=array("a"=>"Student","b"=>"teacher","c"=>"Engg.");

    print_r(array_rand($a,2))."<br/>";

    ?>

</body>

</html>

 

Output

rand.jpg

You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.