How to get a random key from a PHP array – onlinecode

How to get a random key from a PHP array – onlinecode

In this post we will give you information about How to get a random key from a PHP array – onlinecode. Hear we will give you detail about How to get a random key from a PHP array – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this post, i will tell you how to get random key from PHP array.

array_rand method in PHP will return the keys of the random entries.

This method takes two argument : first will be the input array and second argument specifies how many entries you want to picked.

Each time when you refresh the page you will get different number.


Syntax :

array_rand(array,number)


Example :

  1. <?php
  2. $arr=array('a','b','c','d','e','f');
  3. $random_key=array_rand($arr,3);
  4. print_r($random_key);
  5. ?>    
<?php

$arr = array('a','b','c','d','e','f');
$random_key = array_rand($arr, 3);
print_r($random_key);

?>	

Try above example when you need to get random key from given array.

Label :

PHP

How To

Web Development

Hope this code and post will helped you for implement How to get a random key from a PHP array – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

7  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US